I am trying to make a javascript function (although jquery is perfectly OK) that will return a number that corresponds to the number of checkboxes checked in a form. Seems simple enough but I can't figure out a good way of doing it.
Thanks.
I am trying to make a javascript function (although jquery is perfectly OK) that will return a number that corresponds to the number of checkboxes checked in a form. Seems simple enough but I can't figure out a good way of doing it.
Thanks.
Try this:
var formobj = document.forms[0];
var counter = 0;
for (var j = 0; j < formobj.elements.length; j++)
{
if (formobj.elements[j].type == "checkbox")
{
if (formobj.elements[j].checked)
{
counter++;
}
}
}
alert('Total Checked = ' + counter);
.
With JQuery:
alert($('form input[type=checkbox]:checked').size());
var chk = $('form').find('input[type=checkbox]:checked').length
Kind Regards
--Andy
var checkBoxs = $('#myForm').children('input[type="checkbox"]:checked');
alert(checkBoxs.length);