Multiple Checkbox |
You can find many examples on the web, but here goes.
Using normal JavaScript (NP: I have not run this code):
1
2
3
4
5
6
7
8
9
10
11
12
13
| var inputs = document.getElementsByTagName( "input" ); //or document.forms[0].elements; var cbs = []; //will contain all checkboxes var checked = []; //will contain all checked checkboxes for (var i = 0 ; i < inputs.length; i++) { if (inputs[i].type == "checkbox" ) { cbs.push(inputs[i]); if (inputs[i].checked) { checked.push(inputs[i]); } } } var nbCbs = cbs.length; //number of checkboxes var nbChecked = checked.length; //number of checked checkboxes |
Using jQuery:?
1
2
3
4
5
| var cbs = $( "input:checkbox" ); //find all checkboxes var nbCbs = cbs.size(); //the number of checkboxes var checked = $( "input[@type=checkbox]:checked" ); //find all checked checkboxes + radio buttons var nbChecked = checked.size(); |
Sekian, terimakasih atas kunjungannya.
Salam,
PHP Aku : Membangun web terorganisasi dan mudah dimengerti, contoh tutorial dengan menggunakan HTML, CSS, JavaScript, SQL, PHP, dan XML.