How to check a button is clicked or not in JavaScript ?
Last Updated :
12 Sep, 2024
Checking if a button is clicked in JavaScript involves detecting user interactions with the button element. This is typically achieved by adding an event listener, such as onclick or addEventListener(click), which triggers a specific function or action when the button is clicked.
There are two methods to check whether a button is clicked or not, these are:
Method 1: Using onclick Event
The Using the onclick Event approach involves assigning a function directly to the onclick property of a button element. This method triggers the function whenever the button is clicked, allowing you to handle click events easily and directly.
Example: In this example, we Clicking the button triggers myGeeks() which sets the button's onclick handler to show an alert. The alert appears when the button is clicked, indicating it's working.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to check a button is clicked
or not in JavaScript?
</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
input[type="button"] {
padding: 5px 10px;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>
How to check a button is clicked
or not in JavaScript?
</h3>
<input type="button"
class="button"
value="Button"
onclick="myGeeks()">
<script>
function myGeeks() {
document.querySelector(".button").
onclick = function () {
alert("Button Clicked");
}
}
</script>
</body>
</html>
Output:
OutputMethod 2: Using click Event
The Using click Event approach employs addEventListener(click) to attach a click event listener to a button element. This method separates JavaScript logic from HTML, enhancing maintainability. When the button is clicked, the specified function is executed, enabling desired actions.
Example: In this example, we clicking the button triggers the myGeeks() function, which adds a click event listener to the button. This listener shows an alert with "Button Clicked" when activated.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
How to check a button is clicked
or not in JavaScript?
</title>
<style>
body {
text-align: center;
}
h1 {
color: green;
}
input[type="button"] {
padding: 5px 10px;
margin-top: 10px;
}
</style>
</head>
<body>
<h1>GeeksforGeeks</h1>
<h3>
How to check a button is clicked
or not in JavaScript?
</h3>
<input type="button"
class="button"
value="Button"
onclick="myGeeks()">
<script>
function myGeeks() {
document.querySelector(".button")
.addEventListener("click", function () {
alert("Button Clicked");
});
}
</script>
</body>
</html>
Output:
Similar Reads
How to Add event listener to Button in JavaScript ? In web development adding an event listener to a button is a common task that allows us to execute JavaScript code in response to user interactions, such as clicks. This helps us to create interactive and flexible web pages. Using addEventListener()The addEventListener method is a versatile and wide
1 min read
How to count the number of times a button is clicked using JavaScript ? At times, it becomes necessary to monitor the number of times a user clicks a button. In this article, we are going to learn how to count the number of times a button is clicked using JavaScript Below are the approaches to count the number of times a button is clicked using JavaScript Table of Conte
3 min read
How to check whether a radio button is selected with JavaScript ? Here are the different methods used to check the radio button is selected:1. Using Input Radio checked propertyThe checked property in JavaScript checks if a radio button is selected. By using document.getElementById or querySelector to get the radio button, the checked property returns true if the
3 min read
How to check whether an object exists in javascript ? Checking whether an object exists in JavaScript refers to determining if a variable or property has been declared and contains a valid value. This helps avoid errors when accessing or manipulating objects that may be undefined, null, or not initialized properly.Here we have some common approaches to
3 min read
How to Simulate a Click in JavaScript? Simulating a click in JavaScript means triggering a click event on an HTML element using code, rather than requiring manual user interaction. This technique is useful for automating actions like clicking buttons, links, or other interactive elements, enhancing web functionality, and triggering hidde
3 min read