CodyK
CodyK

Reputation: 3667

Issues With Javascript Checkboxes

Hi I am having some issues trying to get check box values. For a school project I have a pizza order form and I am suppose to print a summary of he order. I am having issues printing the toppings.

<label><input type = "checkbox" name = "topping" id = "sausage" value = "Sausage"/>  Sausage</label>
<label><input type = "checkbox" name = "topping" id = "pepperoni" value = "Pepperoni"/>  Pepperoni</label>
<label><input type = "checkbox" name = "topping" id = "beef" value = "Beef"/>  Beef</label>
<label><input type = "checkbox" name = "topping" id = "bacon" value = "Bacon"/>  Bacon</label><br />
<label><input type = "checkbox" name = "topping" id = "chicken" value = "Chicken"/>  Chicken</label>
<label><input type = "checkbox" name = "topping" id = "ham" value = "Ham"/>  Ham</label>
<label><input type = "checkbox" name = "topping" id = "olives" value = "Olives"/>  Olives</label>
<label><input type = "checkbox" name = "topping" id = "peppers" value = "Peppers"/>  Peppers</label><br />
<label><input type = "checkbox" name = "topping" id = "tomatoes" value = "Tomatoes"/>  Tomatoes</label>
<label><input type = "checkbox" name = "topping" id = "mushrooms" value = "Mushrooms"/>  Mushrooms</label>
<label><input type = "checkbox" name = "topping" id = "pineapple" value = "Pineapple"/>  Pineapple</label>

Thats the html part and I have my javascript function where I think the problem is.

function toppings(inputCollection) {
    var toppings = "";

    for (var i = 0; i < inputCollection.length; i++) {

        if (inputCollection[i].checked) {
            toppings = toppings + inputCollection[i].value + " ";
        }
    }

    return toppings;
}

I am fairly new to javascript so please don't flame me if I made a stupid mistake. Thank you very much

Upvotes: 3

Views: 165

Answers (2)

Marc
Marc

Reputation: 11633

Have a look at this example, which also shows how labels are properly used: http://jsbin.com/upeqam

Upvotes: 0

westo
westo

Reputation: 575

How are you calling your function?

This should do it - toppings(document.getElementsByName("topping"))

Upvotes: 1

Related Questions