Need to count checkboxes on a button click on it does not work

Go To StackoverFlow.com

1

The following form has 11 checkboxes.. I set up the script to execute a function on the click of a button that would show a popup telling how many of the checkboxes were checked.. However, when I click the button, nothing happens. I think my mistake may be in the function code and discerning the use of name vs. id etc... But I am not sure about what the mistake was.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<style type="text/css">
body {background-color:Gray;}
body {text-align:left;}
p {text-align:center}

</style>

    <title>My First Project</title>
</head>
<body>
<form id="form1" >

<table align=center>
<tr>
<td><input type="checkbox" name="ingredients" value="sausage" /> Italian Sausage<br /></td>
<td><input type="checkbox" name="ingredients" value="mushrooms" /> Fresh Mushrooms<br /></td>
<td><input type="checkbox" name="ingredients" value="pepperoni" /> Pepperoni<br /></td>
</tr>

<tr>
<td><input type="checkbox" name="ingredients" value="onions" /> Fresh Onions<br /></td>
<td><input type="checkbox" name="ingredients" value="ham" /> Diced Ham<br /></td>
<td><input type="checkbox" name="ingredients" value="peppers" /> Fresh Green Peppers<br /></td>
</tr>

<tr>
<td><input type="checkbox" name="ingredients" value="beef" /> Beef<br /></td>
<td><input type="checkbox" name="ingredients" value="tomatoes" /> Diced Tomatoes<br /></td>
<td><input type="checkbox" name="ingredients" value="bacon" /> Bacon Bits<br /></td>
</tr>

<tr>
<td><input type="checkbox" name="ingredients" value="green" /> Green Olives<br /></td>
<td><input type="checkbox" name="ingredients" value="olives" /> Ripe Olives<br /></td>
</tr>
</table>
<br />

<script type="text/javascript">

        function anyCheck(form) {
            var total = 0;
            var max = form.ckbox.length;
            for (var idx = 0; idx < max; idx++) {
                if (eval("document.form.ckbox[" + idx + "].checked") == true) {
                    total++;
                }
            }
            alert("You selected " + total + " boxes.");
        }


    }
</script>
<input type="button" name="submit" value="Submit" onclick="anyCheck(form1);"  />
</form>
</body>
</html>

UPDATED CODE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<style type="text/css">
body {background-color:Gray;}
body {text-align:left;}
p {text-align:center}

</style>

    <title>My First Project</title>
</head>
<body>
<form id="form1" >

<table align="center">
<tr>
<td><input type="checkbox" name="ingredients" value="sausage" /> Italian Sausage<br /></td>
<td><input type="checkbox" name="ingredients" value="mushrooms" /> Fresh Mushrooms<br /></td>
<td><input type="checkbox" name="ingredients" value="pepperoni" /> Pepperoni<br /></td>
</tr>

<tr>
<td><input type="checkbox" name="ingredients" value="onions" /> Fresh Onions<br /></td>
<td><input type="checkbox" name="ingredients" value="ham" /> Diced Ham<br /></td>
<td><input type="checkbox" name="ingredients" value="peppers" /> Fresh Green Peppers<br /></td>
</tr>

<tr>
<td><input type="checkbox" name="ingredients" value="beef" /> Beef<br /></td>
<td><input type="checkbox" name="ingredients" value="tomatoes" /> Diced Tomatoes<br /></td>
<td><input type="checkbox" name="ingredients" value="bacon" /> Bacon Bits<br /></td>
</tr>

<tr>
<td><input type="checkbox" name="ingredients" value="green" /> Green Olives<br /></td>
<td><input type="checkbox" name="ingredients" value="olives" /> Ripe Olives<br /></td>
</tr>
</table>
<br />

<script type="text/javascript">

    function anyCheck() {
    var form = document.getElementById("form1"),
        inputs = form.getElementsByTagName("input"),
        i,
        total = 0;

    for (i = 0; i < inputs.length; i++) {
        if (inputs[i].type.toLowerCase() === "checkbox" && inputs[i].checked)
            total++;
    }
    alert("You selected " + total + " boxes.");
}


    }
</script>
<input type="button" name="submit" value="Submit" onclick="anyCheck();"  />
</form>
</body>
</html>
2012-04-04 21:33
by Wilson


0

There's a problem in your function call. form1 is undefined.

Use document.getElementById("form1"); instead, like this:

onclick="anyCheck(document.getElementById('form1'));"

Edit: There's some more problems as well:

There's no ckbox array. Either create an array for that, or use the form.elements array (which will give you all form elements, then you can check to see if it's a checkbox or not).

And you don't need to call eval.

        for (var idx = 0; idx < max; idx++) {
            if (form.ckbox[idx].checked == true) {
                total++;
            }
        }
2012-04-04 21:37
by Simon Forsberg


0

This works in newer browsers (IE8+,FF 3.5+,Chrome):

function anyCheck() {
        var total = 0;
        var max = document.querySelectorAll('[type=checkbox]');
        for (var idx = 0; idx < max.length; idx++) {
            if (max[idx].checked) {
                total++;
            }
        }
        alert("You selected " + total + " boxes.");
 }

DEMO: http://jsfiddle.net/6pNjf/1/

2012-04-04 21:41
by binarious


0

There are a couple problems:

You need to pass in either a string or an element into anyCheck. I would do a string.

onclick="anyCheck('form1')"

Inside of anyCheck, you should first get the actual instance of the form into a variable, like this:

form = document.getElementById(form);

You can then loop through each of the items in the form using getElementsByTagName()

var inputs = form.getElementsByTagName("input");

for(var x = 0; x < inputs.length; x++) {
    var input = inputs[x];
    if(input.type != "checkbox") continue;

    if(input.checked) {
        total += 1;
    }
}
2012-04-04 21:42
by Jordan


0

As the other answers said, the parameter in anyCheck() needs to be a reference to the form. I'd pass in the id as a string and use document.getElementById() inside the function. Or, if there is only one form, don't even have it as a parameter, something like this:

function anyCheck() {
    var form = document.getElementById("form1"),
        inputs = form.getElementsByTagName("input"),
        i,
        total = 0;

    for (i = 0; i < inputs.length; i++) {
        if (inputs[i].type.toLowerCase() === "checkbox" && inputs[i].checked)
            total++;
    }
    alert("You selected " + total + " boxes.");
}

http://jsfiddle.net/ZxyPW/

Your code was trying to use a ckbox array that doesn't exist. What I've shown is how to select all input elements, then test if each one is a checkbox that is checked.

You don't need to use eval at all, certainly not to use an index variable in an array (or array-like object).

2012-04-04 21:48
by nnnnnn
I posted my updated code, but it still doesn't work. - Wilson 2012-04-04 22:01
You have an extra closing } just before your closing </script> tag (in both your original and updated code) - that's a syntax error that is probably stopping the script running at all - nnnnnn 2012-04-04 22:23


0

Try this code instead.

You got a few problems with you were passing in the form and how you were accessing the elements within the form. I also highly suggest you get IDE with javascript debugger built in. VS 2010 is a free one that you can use to develop html or asp.net code. You can stick with straight html code if you like. I.E. has a built in one if you turn on the correct options.

To use the debugger you can uncomment the //debugger line

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <style type="text/css">
        body
        {
            background-color: Gray;
        }
        body
        {
            text-align: left;
        }
        p
        {
            text-align: center;
        }
    </style>
    <title>My First Project</title>
</head>
<body>
    <form id="form1">
    <table align="center">
        <tr>
            <td>
                <input type="checkbox" name="ingredients" value="sausage" />
                Italian Sausage<br />
            </td>
            <td>
                <input type="checkbox" name="ingredients" value="mushrooms" />
                Fresh Mushrooms<br />
            </td>
            <td>
                <input type="checkbox" name="ingredients" value="pepperoni" />
                Pepperoni<br />
            </td>
        </tr>
        <tr>
            <td>
                <input type="checkbox" name="ingredients" value="onions" />
                Fresh Onions<br />
            </td>
            <td>
                <input type="checkbox" name="ingredients" value="ham" />
                Diced Ham<br />
            </td>
            <td>
                <input type="checkbox" name="ingredients" value="peppers" />
                Fresh Green Peppers<br />
            </td>
        </tr>
        <tr>
            <td>
                <input type="checkbox" name="ingredients" value="beef" />
                Beef<br />
            </td>
            <td>
                <input type="checkbox" name="ingredients" value="tomatoes" />
                Diced Tomatoes<br />
            </td>
            <td>
                <input type="checkbox" name="ingredients" value="bacon" />
                Bacon Bits<br />
            </td>
        </tr>
        <tr>
            <td>
                <input type="checkbox" name="ingredients" value="green" />
                Green Olives<br />
            </td>
            <td>
                <input type="checkbox" name="ingredients" value="olives" />
                Ripe Olives<br />
            </td>
        </tr>
    </table>
    <br />
    <script type="text/javascript">

        function anyCheck(form) 
        {
            //debugger;
            var total = 0;
            var max = form.length;
            for (var idx = 0; idx < max; idx++)
            {
                var element = form[idx];
                if(element.type == "checkbox" && element.checked)                
                {
                    total++;
                }
            }
            alert("You selected " + total + " boxes.");
        }
    </script>
    <input type="button" name="submit" value="Submit" onclick="anyCheck(document.getElementById('form1'));" />
    </form>
</body>
</html>
2012-04-04 21:52
by Dan P
Ads