How to use an HTML button to call a JavaScript function?


We use the onclick event attribute property of the HTML button to call a JavaScript function. The JavaScript code provided in the onclick attribute executes when the button is clicked. There are various attributes provided with the button tag in HTML that allows us to customize the button’s functionality and also let us decide how and what the button triggers.

Approach 1: Using the onclick event in JavaScript

The onclick event of the button element expects JavaScript code that is triggered when the button is clicked upon. So we put the function that needs to be called in the onclick property as well.

Syntax

<button onclick = "fun()" > Click Me </button>

This creates an HTML button with the name "click me" and triggers the "fun()" function.

Example 1

Here we will use an HTML button to call a JavaScript function. The associated function body executes when the button is clicked. Let’s look at the code for same.

<!DOCTYPE html> <html> <title>Online Javascript Editor</title> <head> </head> <body> <script> function fun() { document.getElementById("result").innerHTML = "The function fun() is triggered !"; } </script> Calling js function using HTML button <br><br> <button onclick = "fun()"> click me !</button> <p> <div id = "result"> </div> </p> </body> </html>

In the above code, The function fun() is triggered when the button is clicked.

Approach 2: Using the ondblclick event in JavaScript

More options are provided to customize the execution of the JavaScript functions in different ways. for example, we can also set the function to be called only when the button is double-clicked. This can be done with the "ondblclick" event of the button tag.

Syntax

<button ondblclick = "fun()" > Click Me 

This creates an HTML button with the name "Button_Name" and triggers the "fun()" function when the button is double-clicked.

Example 2

Here we will use an HTML button to call a JavaScript function. The associated function body executes when the button is double-clicked.

<!DOCTYPE html> <html> <title>Online Javascript Editor</title> <head> </head> <body> <script> function fun() { document.getElementById("result").innerHTML = "The function fun() is triggered !"; } </script> <h3>Calling js function using HTML button <br></h3> <p> Double click "click me!" button </p> <button ondblclick = "fun()"> click me ! </button> <p> <div id = "result"> </div> </p> </body> </html>

In the above code, we have the "click me" button that triggers the fun() function when it is double clicked.

Approach 3: Using the onclick event of an input button

Buttons can also be part of forms that do some sort of validation and form submission. Buttons can also be created using the input tag provided by HTML. The onclick event attribute is again configured to handle the behavior of the button.

Syntax

<input type = "button" onclick = "fun()" value = "Button_Name" >

This creates an HTML button with the name "Button_Name" and triggers the "fun()" function.

Let us look at an example to see this use case.

Example 3

We will create a button for submitting a mock form, this button triggers the JavaScript function provided in the onclick property.

<!DOCTYPE html> <html> <title>Online Javascript Editor</title> <head> </head> <body> <script> function fun() { document.getElementById("result").innerHTML = "The function fun() is triggered !"; } </script> Calling js function using HTML button <br><br> <form> <label> Name : </label> <input type = "text"> </input><br><br> <input type = "button" onclick = "fun()" value = "submit"> </form> <p> <div id = "result"> </div> </p> </body> </html>

In the above code, The function fun() is triggered when the submit button is clicked.

Approach 4: Using jQuery

As an alternative we can also use jQuery to attach the function to the button programmatically.

Syntax

$(document).ready(function() {      
   $('#Your_Button').click(function() { 
      fun(); 
   }); 
});

This jQuery script checks for the readiness of the document and then attaches the function fun() to the click of the button having id as "Your_Button".

Let us look at an example to see this use case.

Example 4

We will create a HTML button and attach an event handler "onclick" to it programmatically using jQuery. Note that this attachment happens after the complete document has been rendered successfully.

The script in head tag imports the jQuery.

<!DOCTYPE html> <html> <title>Online Javascript Editor</title> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> </head> <body> Calling js function using HTML button <br><br> <button id="button"> click me !</button> <p> <div id="result"> </div> </p> <script> $(document).ready(function() { $('#button').click(function() { fun(); }); }); function fun() { document.getElementById("result").innerHTML = "The function fun() is triggered !"; } </script> </body> </html>

Conclusion

The onclick property of HTML buttons are a fast and effective way of attaching JavaScript functions to them.

Updated on: 06-Sep-2023

49K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements