How to Click Button Using JavaScript?

Buttons on a web-page allows users to submit data or to do any action on it and generally the actions are performed by clicking on it. Different types of buttons are used on web-page and they may be inside a form or outside a form for certain action to be performed. Buttons used within a form may be Submit Button, Reset Button, Browse Button etc.

You can set the actions performed by submit and reset buttons on a form with in a
tag as below.

onreset="return conform()" method="post">





In the above code, when submit button was clicked it goes to the page "submitpage.html" after validating the form by the function validate() and when reset button was clicked it clears all the form data after displaying conformation box by the function conform().

It is easier to do any task on clicking event of a button by executing certain JavaScript functions when the button was clicked. There are different ways to execute JavaScript codes on button click. In this post I am going to describe about those different methods of clicking buttons using JavaScript.

Different Ways to Click Button Using JavaScript


You can click button using JavaScript with the different methods given below.

With directly specifying JavaScript Built-in function on onClick event of Button


onClick="alert(new Date().toLocaleTimeString());">


Preview:


With creating a JavaScript function and specifying it on onClick event of Button as given on the previous post: "How to create Timer Using JavaScript?"








Preview:
 

With creating a JavaScript function along with specifying onClick event of Button






Preview:
 

With specifying single function for multiple buttons having same class name by using for loop on onClick event of button










Preview:
 

I have described here some methods of clicking buttons using JavaScript . If you know other more methods of clicking buttons using JavaScript, you are welcomed to mention on the comment session at the bottom this post.



Related Search Terms


Related Posts: