Javascript Form How to - Set focus to checkbox








Question

We would like to know how to set focus to checkbox.

Answer


<!--   ww w  . j  a  va  2s  . c  o m-->
<html>
    <form name="myForm">
      <input type="checkbox" name="checkBox1">Option 1<br>
      <input type="checkbox" name="checkBox2">Option 2<hr>
      <input type="button" name="focusButton" value="Move Focus" onClick="moveFocus()">
    </form>
    <script language="JavaScript">
    function moveFocus() {
      document.myForm.checkBox1.focus();
    }
    </script>
</html>

The code above is rendered as follows: