Javascript Form How to - Get CheckBox value








Question

We would like to know how to get CheckBox value.

Answer


<!--   w  ww .j  ava2 s  . c  o  m-->
<html>
    <form name="orderForm">
      <input type="checkbox" name="onion" value="hot onion">Onion<br>
      <input type="checkbox" name="bacon" value="spicy bacon">Bacon<hr>
      <input type="button" value="Order Pizza" name="orderButton" onClick="console.log('ordered.')">
    </form>
    <script language="JavaScript">
    document.write(document.orderForm.onion.value);   
    document.write(" or ",document.orderForm.bacon.value);  
    </script>
</html>

The code above is rendered as follows: