JavaScript click method

In the html page we can select the check box either by clicking on the checkbox option while selecting any option or we can call the JavaScript method click() on the checkbox object.

JavaScript click method

JavaScript click method

     

In the html page we can select the check box either by clicking on the checkbox option while selecting any option or we can call the JavaScript method click() on the checkbox object. In this example we have developed an example which shows how one can select the checkbox by using the method click.

Syntax:

 CheckBoxObject.click();

Description of code:

Here in this example html code we have create three check boxes and three buttons. In these three checkboxes we have called the three different functions selectFirst(), selectSecond(), selectThird().All these three methods calls the click method with the checkbox objects.

Here is the full example source code of clickExample.html as follows:

<html>
<body>
<script language="JavaScript">
  function selectFirst() {
   checkbox1.focus(); 
   checkbox1.click();
  }
  function selectSecond() {
   checkbox2.focus(); 
   checkbox2.click();
  }
  function selectThird() {
   checkbox3.focus(); 
   checkbox3.click();
  }
</script>
   <p>&nbsp;</p>
   <p align="center">&nbsp;</p>
   <div style="background: #cf2255; width:'100%';" align="center">
   <font color="#ffffcc" size="12pt"><b>Click Example</b></font></div>
   <center>
   <p align="left">
   <input type="checkbox" id="checkbox1" value="Checkbox1">
   <button onclick="selectFirst();">Click first checkbox</button>
  </p>
  <p align="left">
  <input type="checkbox" id="checkbox2" value="Checkbox1">
  <button onclick="selectSecond();">Click second checkbox</button>
  </p>

  <p align="left">
  <input type="checkbox" id="checkbox3" value="Checkbox3">
  <button onclick="selectThird();">Click third checkbox</button>
  </p>
  </body>
</html>

Output :

Download Source Code