Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Since all other answers are jquery based, I'll offer a pure javascript solution. Assuming the following form:</p> <pre><code>&lt;form id="myform"&gt; &lt;input type="checkbox" value="username1" name="check[0]" /&gt;&lt;br/&gt; &lt;input type="checkbox" value="userusername2" name="check[1]" /&gt;&lt;br/&gt; &lt;input type="checkbox" value="userusername3" name="check[2]" /&gt;&lt;br/&gt; &lt;/form&gt; </code></pre> <p>You could compute the number of checkbox elements with the following logic:</p> <pre><code>&lt;script type="text/javascript"&gt; var myform = document.getElementById('myform'); var inputTags = myform.getElementsByTagName('input'); var checkboxCount = 0; for (var i=0, length = inputTags.length; i&lt;length; i++) { if (inputTags[i].type == 'checkbox') { checkboxCount++; } } alert(checkboxCount); &lt;/script&gt; </code></pre> <p>BTW: As others have noted, the <code>id</code> attribute in any HTML tag should be unique within the document. I've omitted your <code>id="1"</code> attributes in my sample HTML above.</p> <h2>Update:</h2> <p>If you simply want to count all checkbox elements on the entire page without using a containing form element, this should work:</p> <pre><code>&lt;script type="text/javascript"&gt; var inputTags = document.getElementsByTagName('input'); var checkboxCount = 0; for (var i=0, length = inputTags.length; i&lt;length; i++) { if (inputTags[i].type == 'checkbox') { checkboxCount++; } } alert(checkboxCount); &lt;/script&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload