jQuery click() Method
jQuery click() Method
The jQuery click()
method is used for the following 2 purpose
- To run a callback when a specified element is clicked.
- To trigger the click event of a specified element.
Run a Callback
In the following example, we will run a callback function when a button is clicked.
Example
HTML Editor
<!DOCTYPE html>
<html lang="en-US">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>jQuery click() Method</h1>
<p>Click on the button to alert</p>
<button>Click Me</button>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("button clicked");
});
});
</script>
</body>
</html>
Syntax
$(selector).click(callback);
Parameter Values
Value | Type | Explanation |
---|---|---|
callback | Optional | Specifies a function to be called once the element is clicked. |
Trigger a Click Event
In the following example, we will trigger a click event when you select the text in the input.
Example
HTML Editor
<!DOCTYPE html>
<html lang="en-US">
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
</head>
<body>
<h1>jQuery click() Method</h1>
<input type="text" value="select me">
<button>Click Me</button>
<script>
$(document).ready(function(){
$("button").click(function(){
alert("button clicked");
});
$("input").select(function(){
$("button").click();
});
});
</script>
</body>
</html>
Reminder
Hi Developers, we almost covered 99.5% of jQuery Tutorials with examples for quick and easy learning.
We are working to cover every Single Concept in jQuery.
Please do google search for:
Join Our Channel
Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.
This channel is primarily useful for Full Stack Web Developer.