Javascript Form How to - Check if checkbox is selected








Question

We would like to know how to check if checkbox is selected.

Answer


  <!-- www . ja  va2  s  . com-->
<html>
<head>
  <script type="text/javascript">
    function showWindow() {
      var txt = document.form1.stringField.value;
      var clr = "";
      var sze = "";

      if (document.form1.bigBox.checked) txt = txt.big();
      if (document.form1.boldBox.checked) txt = txt.bold();
      if (document.form1.fixedBox.checked) txt = txt.fixed();

      objWindow = window.open("", "","width=600,height=300");
      objWindow.document.write(txt);
      objWindow.document.close();
  }
  </script>
</head>
<body>
  <form method="post" name="form1" action="null">
      String:<input type="text" size="40" maxlength="256" name="stringField" />
      Style:
      <input type="checkbox" name="bigBox" value="ON" />Big
      <input type="checkbox" name="boldBox" value="ON" />Bold
      <input type="checkbox" name="fixedBox" value="ON" />Fixed
    <input type="button" name="Show" value="Show" onclick="showWindow()" />
  </form>
</body>
</html>

The code above is rendered as follows: