user1148875 user1148875 - 1 month ago 11
Javascript Question

How can get length on checked checkbox array javascript

<form name='form1' >
<input type=checkbox name='cbox[]' />
</form>

<script>
var checkbox = document.getElementsByName('ckbox[]')
var ln = checkbox.length
alert(ln)
</script>


How can I count only the checked checkboxes with javascript or jQuery?

Answer

Doing it with jQuery would shorten the code and make it more readable and maintable. Use attribute selector with :checked selector

Live Demo

$('[name="cbox[]"]:checked').length