jQuery API

.click()

.click( handler(eventObject) ) Returns: jQuery

Description: Bind an event handler to the "click" JavaScript event, or trigger that event on an element.

  • version added: 1.0.click( handler(eventObject) )

    handler(eventObject)A function to execute each time the event is triggered.

  • version added: 1.0.click()

This method is a shortcut for .bind('click', handler) in the first variation, and .trigger('click') in the second.

The click event is sent to an element when the mouse pointer is over the element, and the mouse button is pressed and released. Any HTML element can receive this event.

For example, consider the HTML:
<div id="target">
  Click here
</div>
<div id="other">
  Trigger the handler
</div>

The event handler can be bound to any <div>:

$('#target').click(function() {
  alert('Handler for .click() called.');
});

Now if we click on this element, the alert is displayed:

Handler for .click() called.

We can also trigger the event when a different element is clicked:

$('#other').click(function() {
  $('#target').click();
});

After this code executes, clicks on Trigger the handler will also alert the message.

The click event is only triggered after this exact series of events:

  • The mouse button is depressed while the pointer is inside the element.
  • The mouse button is released while the pointer is inside the element.

This is usually the desired sequence before taking an action. If this is not required, the mousedown or mouseup event may be more suitable.

Examples:

Example: To hide paragraphs on a page when they are clicked:

<!DOCTYPE html>
<html>
<head>
  <style>
  p { color:red; margin:5px; cursor:pointer; }
  p.hilite { background:yellow; }
  </style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
	<p>First Paragraph</p>

  <p>Second Paragraph</p>
  <p>Yet one more Paragraph</p>
<script>
    $("p").click(function () { 
      $(this).slideUp(); 
    });
    $("p").hover(function () {
      $(this).addClass("hilite");
    }, function () {
      $(this).removeClass("hilite");
    });
</script>
</body>
</html>

Demo:

Example: To trigger the click event on all of the paragraphs on the page:

$("p").click();

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for .click() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • Dejan
    If you have a child inside an element. And both the parent and the child have their own click handlers, when clicking on the child element, both click handlers are triggered.

    Dont know if that's a bug, or is it as it should to be, but its pretty much ugly.
  • aandras72
    If you have a situation where you have defined a javascript function that attaches an event through the click function described here, and this function gets executed multiple times, will the function overwrite the click event or will it append to the click event. That is:

    $("a").click(fucntion() {
    doSomething();
    }

    Suppose this gets executed multiple times (say 3 times). Will the result be a call to doSomething() once, or doSomething() three times?

    Thanks in advance!
  • It's important to remember that click() will not trigger the default behavior on a link, even if nothing else is preventing it. So you can't use click() by itself to simulate the user clicking on a link and being taken to another url.
  • bobbykjack
    According to my testing, under Firefox 3, this event also fires on a submit button when that button's form is submitted, even if it's submitted via the keyboard. This is quite non-intuitive, so be sure it doesn't catch you out!