Note that there are some explanatory texts on larger screens.

plurals
  1. POI can't figure out what I'm missing, code works 100% except for one option
    text
    copied!<p>The code below adds totals of check boxes and gives a discount if ALL are checked. And I got it working great, but for some reason when I use the "CHECK ALL" button, and then manually uncheck one of the boxes, it sets the total to -119, rather than to 476. I'm sure I'm missing something, but I can't seem to figure it out. Any help would be appreciated! </p> <p>Thanks.</p> <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;link rel="shortcut icon" type="image/x-icon" href="../favicon.ico" /&gt; &lt;link rel="icon" type="image/x-con" href="../favicon.ico" /&gt; &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt; &lt;!-- Begin JavaScript --&gt; &lt;!--- ADDING THE CHECK BOXES WITH SELECT ALL DISCOUNT ---&gt; &lt;script language="javascript" type="text/javascript"&gt; //Define global form total: var form_amount=0; //Define function to check if all boxes are selected: function allChecked() { for (xx=0; xx &lt; document.WisBundle.elements.length; xx++) { if (document.WisBundle.elements[xx].type == 'checkbox' &amp;&amp; document.WisBundle.elements[xx].checked == false) return false; if (checked == false){ form_amount = 0; for (i = 0; i &lt; document.WisBundle.elements.length; i++) { if (document.WisBundle.elements[i].type == 'checkbox') { form_amount += parseFloat(document.WisBundle.elements[i].value); } } checked = true; document.WisBundle.amount.value = form_amount; } } return true; } //Define function to manipulate the form total per item selected/deselected: function CheckChoice(whichbox) { //If box was checked, accumulate the checkbox value as the form total, //Otherwise, reduce the form total by the checkbox value: if (whichbox.checked == false) { form_amount -= eval(whichbox.value); } else { form_amount += eval(whichbox.value); } //Re-set displayed total on form: document.WisBundle.amount.value = eval(form_amount); //If all the boxes are checkec, display the total as 999.00 giving them a discount: if(allChecked()) document.WisBundle.amount.value = '999.00'; else //Re-set displayed total on form: document.WisBundle.amount.value = eval(form_amount); } //Define function to init the form on reload: function InitForm() { //Reset the displayed total on form: document.WisBundle.amount.value='0'; //Set all checkboxes on form to unchecked: for (xx=0; xx &lt; document.WisBundle.elements.length; xx++) { if (document.WisBundle.elements[xx].type == 'checkbox') { document.WisBundle.elements[xx].checked = false; } } } //Check and uncheck all checkboxes: checked=false; function checkedAll () { var aa= document.WisBundle; if (checked == false) { checked = true document.WisBundle.amount.value = '999.00'; } else { checked = false document.WisBundle.amount.value = '0'; form_amount = 0; } //white checked is true do this for (var i =0; i &lt; aa.elements.length; i++) { aa.elements[i].checked = checked; } } &lt;/script&gt; &lt;title&gt;Add Boxes&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="BLANKPAGE_TEST_2.cfm" method="post" id="addCommentForm" name="WisBundle" onsubmit="return submit_form()"&gt; &lt;input type="button" name="UnCheckAll" value="Check / Uncheck All" onclick='checkedAll();'&gt; &lt;!--- &lt;label&gt;&lt;input type='checkbox' name='checkall' onclick='checkedAll();'&gt; Select All&lt;/label&gt; ---&gt; &lt;hr /&gt; &lt;table id="commentTable"&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Item One&lt;/label&gt;&lt;/th&gt; &lt;td&gt; &lt;label&gt;&lt;input type="checkbox" name="ItemOne" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /&gt; $119&lt;/label&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Item Two&lt;/label&gt;&lt;/th&gt; &lt;td&gt; &lt;label&gt;&lt;input type="checkbox" name="ItemTwo" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /&gt; $119&lt;/label&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Item Three&lt;/label&gt;&lt;/th&gt; &lt;td&gt; &lt;label&gt;&lt;input type="checkbox" name="ItemThree" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /&gt; $119&lt;/label&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Item Four&lt;/label&gt;&lt;/th&gt; &lt;td&gt; &lt;label&gt;&lt;input type="checkbox" name="ItemFour" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /&gt; $119&lt;/label&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;&lt;label&gt;Item Five&lt;/label&gt;&lt;/th&gt; &lt;td&gt; &lt;label&gt;&lt;input type="checkbox" name="ItemFive" value="119.00" onclick="CheckChoice(this);" onfocus="startCalc();" onblur="stopCalc();" class="checkbox" /&gt; $119&lt;/label&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th class="altTH"&gt;&lt;label&gt;Total $:&lt;/label&gt;&lt;/th&gt; &lt;td class="altTD"&gt;&lt;input type="text" name="amount" readonly="readonly" class="inputSmall" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&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