The jQuery click () method is executed once the click event occurs.
Syntax:
To trigger the click event for the selected elements.
$(selector).click()
To attach a function to the click event.
Example1:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("h1,h2").click(function(){ $(this).hide(); }); }); </script> </head> <body> <h1>Click me. I am a ghost.</h1> <h2>Me too.</h2> </body> </html>
Example2:
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ alert("Width: " + $("div").width()); }); }); </script> </head> <body> <div style="height:100px;width:500px;padding:10px;margin:3px;border:1px solid blue;background-color:lightpink;"></div><br> <button>Width</button> </body> </html>