19 Feb jQuery click() method
Definition
Use the jQuery click() method to trigger an event on the click of an element. For example, on click, it generates an alert box.
Syntax
The following is the syntax,
1 2 3 |
$(selector).click(func) |
The following is the parameter used in the jQuery click() method,
- func: When event occur, this function runs.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("h2").click(function(){ alert("You clicked me."); }); }); </script> </head> <body> <h2>I am a heading. I love my life. Click on me.</h2> <p>Studyopedia provides free tutorials for all.</p> </body> </html> |
The following is the output,
Click the heading to fire click event,
No Comments