jQuery click | How to use mouse click() event method in jQuery

What is the jQuery click() method?

The click event occurs when an element of HTML document is clicked. For example clicking a button, a paragraph, a Div, a link or an image etc. As click event occurs, an event handler function can be attached to perform a certain action with the click() method.

How to use the click method

$(selector).click(function)

Where the function is optional. It runs as click event occurs.

e.g.

  • $(“p”).click(function)
  • $(“div”).click(function)
  • $(“button”).click(function)

Below are running examples of click() method with different HTML elements

Example of click() with paragraph

In the example below, as the first paragraph is clicked, it will trigger an event handler that will show an alert. The click method is attached with clickme id.

Copy and paste code into HTML file and run in your browser.

jQuery click

Experience this example online

Example of jQuery click() with Div <div>

Just like the above example except the div tags are used for demonstrating the click jQuery method:

Experience this example online

Also see jQuery events