CheckBox Calculator

Check boxes are stand-alone elements; that is, they don't interact with neighboring elements like radio buttons do. Therefore they are a bit easier to use. Using JavaScript you can test if a check box is checked or not using the checked property. Likewise, you can set the checked property to add or remove the checkmark from a check box.

Value:
Action: (default double/half)
Square/Square Root
Result:


<SCRIPT>

function calculate(form, callingField) {
     if (callingField == "result") { // is the calling field the "result" field?
          // is the "square" check box checked?
          if (form.square.checked) form.entry.value = Math.sqrt(form.result.value);
          else form.entry.value = form.result.value / 2;
      }
      else {
               if (form.square.checked) form.result.value = form.entry.value * form.entry.value;
               // if (form.square.checked) form.result.value = Math.pow(form.entry.value, 2)
               else form.result.value = form.entry.value * 2;
     }
}

</SCRIPT>

<FORM>
     Value:
     <INPUT TYPE="text" NAME="entry" VALUE="0"
    
  onChange="calculate(this.form, this.name);">
     Action: (default double/half)
     <INPUT TYPE="checkbox" NAME="square"
      
onClick="calculate(this.form, this.name);">
     Square/Square Root Result:
     <INPUT TYPE="text" NAME="result" VALUE="0"
    
  onChange="calculate(this.form, this.name);">
</FORM>

calculate(this.form, this.name) passes 2 parameters:

this.form -- because we want to pass all the form's information to the function
this.name -- to tell the function what particular form element made the call