Javascript Form How to - Simulate the input Button click event








Question

We would like to know how to simulate the input Button click event.

Answer


    <!-- w  ww. j a  va  2 s  . com-->
<html>
    <script language="JavaScript">
    function clickFirstButton()
    {
      document.myForm.button1.click();
    }
    </script>
    <form name="myForm">
      <input type="button"
             value="Display alert box"
             name="button1"
             onClick="console.log('You clicked the first button.')"><br>
      <input type="button"
             value="Call on button 1"
             name="button2"
             onClick="clickFirstButton()">
    </form>    
</html>

The code above is rendered as follows: