Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
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.
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
Here we will use an HTML button to call a JavaScript function. The associated function body executes when the button is clicked.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Button Example</title>
</head>
<body>
<h3>Calling JavaScript function using HTML button</h3>
<button onclick="fun()">Click me!</button>
<p>
<div id="result"></div>
</p>
<script>
function fun() {
document.getElementById("result").innerHTML = "The function fun() is triggered!";
}
</script>
</body>
</html>
In the above code, The function fun() is triggered when the button is clicked.
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</button>
This creates an HTML button and triggers the "fun()" function when the button is double-clicked.
Example
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>
<head>
<title>JavaScript Double Click Example</title>
</head>
<body>
<h3>Calling JavaScript function using HTML button</h3>
<p>Double click "click me!" button</p>
<button ondblclick="fun()">Click me!</button>
<p>
<div id="result"></div>
</p>
<script>
function fun() {
document.getElementById("result").innerHTML = "The function fun() is triggered!";
}
</script>
</body>
</html>
In the above code, we have the "click me" button that triggers the fun() function when it is double clicked.
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.
Example
We will create a button for submitting a mock form, this button triggers the JavaScript function provided in the onclick property.
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Input Button Example</title>
</head>
<body>
<h3>Calling JavaScript function using HTML input button</h3>
<form>
<label>Name:</label>
<input type="text"><br><br>
<input type="button" onclick="fun()" value="Submit">
</form>
<p>
<div id="result"></div>
</p>
<script>
function fun() {
document.getElementById("result").innerHTML = "The function fun() is triggered!";
}
</script>
</body>
</html>
In the above code, The function fun() is triggered when the submit button is clicked.
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".
Example
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.
<!DOCTYPE html>
<html>
<head>
<title>jQuery Button Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<h3>Calling JavaScript function using jQuery</h3>
<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>
Comparison of Approaches
| Method | Event Type | Use Case |
|---|---|---|
| onclick | Single click | Most common button interactions |
| ondblclick | Double click | Special actions requiring confirmation |
| Input button | Single click | Form submissions and validations |
| jQuery | Single click | Dynamic event binding after page load |
Conclusion
The onclick property of HTML buttons provides a fast and effective way of attaching JavaScript functions to them. Choose the appropriate method based on your specific requirements and whether you need simple inline handlers or dynamic event binding.
