Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The code below works ok (a self contained web page). The problem is how to get the array (group) of checkboxes when they're called different names. If you use jquery you could give them all the same class, then get hold of them by that class, but if you're using bare javascript then you can get the elements by Tag name ("input" in the case of the checkbox), and check each one has a name attribute that starts with "choice", inoring those that don't start with "choice", like buttons (also an input) or maybe other checkboxes with different names. It's a bit inefficient if the page is huge, unless you group the checkboxes some way.</p> <p>To group them, you cold put them in a tag like</p> <pre><code>`&lt;div id="checkboxes"&gt; (checkboxes go here) &lt;/div&gt;` </code></pre> <p>then use </p> <pre><code>`var cb = document.getElementById("checkboxes");` `var arrInputs =cb.getElementsByTagName("input");` </code></pre> <ul> <li>for the line to get the arrInputs array. This would just get input type elements from within the Div. Hwever I dind't want to assume the page layout allows your checkboxes to be put in one div</li> </ul> <p>Hope this helps Doug </p> <pre><code>&lt;script type="text/javascript"&gt; function checkTotal() { document.forms.listForm.total.value = ''; var sum = 68.50; var frm=document.forms.listForm; // wasnt sure what your original listForm element was so I've put this form into a variable, frm frm.total.value = ''; var arrInputs =document.getElementsByTagName("input"); // get all Input type elements on the form for (i=0; i &lt; arrInputs .length;i++) { if (arrInputs[i].name.substr(0,6) == "choice") { // if the name starts with "choice" if (arrInputs[i].checked) { sum = sum + parseFloat(arrInputs[i].value); } } } frm.total.value = sum.toFixed(2); } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form name="listForm"&gt; &lt;a href='javascript:checkTotal()'&gt;check&lt;/a&gt;&lt;br&gt; &lt;input type=checkbox name="choice1" value="1"&gt;&lt;br&gt; &lt;input type=checkbox name="choice2" value="2"&gt;&lt;br&gt; &lt;input type=checkbox name="choice3" value="3"&gt;&lt;br&gt; &lt;input type=checkbox name="choice4" value="4"&gt;&lt;br&gt; &lt;input type=checkbox name="choice5" value="5"&gt;&lt;br&gt; &lt;input type=checkbox name="choice6" value="6"&gt;&lt;br&gt; &lt;br&gt; &lt;input type=text name=total value=""&gt;&lt;br&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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