Search code examples
javascriptdom-events

How many check boxes are checked


I have check buttons on the document.

<input type="checkbox" id="CustmerRequested"/>

I want to know how many of the boxes in the document are checked.

I tried to do alert(document.all.CustmerRequested.checked.length); but it says undefined.

How can I find out how many boxes are checked?


Solution

  • If you are starting to build a site that needs this kind of browser-side programming reguarly, I would suggest looking at jQuery. See here for tutorials: http://docs.jquery.com/Tutorials

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#someButton").click(function() {
                var checkedBoxes = $("#yourForm input:checked");
                alert(checkedBoxes.length + " checked.");
            });
        });
    </script>