Javascript Form How to - Get CheckBox default checked value








Question

We would like to know how to get CheckBox default checked value.

Answer


<!--  w w w.java  2  s .  c om-->
<html>
    <form name="orderForm">
      <input type="checkbox" name="door4">4 doors<br>
      <input type="checkbox" name="door2" checked>2 doors<hr>
      Step 2:
      <input type="button" value="Submit Order" name="orderButton" onClick="console.log('submitted')">
      <input type="button" value="Reset Checkboxes" name="resetButton" onClick="resetCheckboxes()">
    </form>
    <script language="JavaScript">
    function resetCheckboxes()
    {
      document.orderForm.door4.checked = document.orderForm.door4.defaultChecked;

      document.orderForm.door2.checked = document.orderForm.door2.defaultChecked;
    }
    </script>
</html>

The code above is rendered as follows: