87

Currently with jQuery when I need to do something when a Click occurs I will do it like this...

$(".close-box").click( function() {
    MoneyBox.closeBox();
    return false;
});

I was looking at some code someone else has on a project and they do it like this...

$(".close-box").live("click", function () {
    MoneyBox.closeBox();
    return false;
});

Notice it seems to do the same thing as far as I can tell except they are using the live() function which is now Deprecated and jQuery docs say to use on() instead but either way why use live/on() instead of my first example?

JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 3
    Besides functionality, I prefer `on` to `click` because `click` as a function name sounds like a verb, an action, when what it does is *not* to click the element (like you can also do with the same `click` function) – 1j01 Apr 01 '18 at 18:55

10 Answers10

154

Because you might have a dynamically generated elements (for example coming from an AJAX call), you might want to have the same click handler that was previously bound to the same element selector, you then "delegate" the click event using on() with selector argument

To demonstrate:

http://jsfiddle.net/AJRw3/

on() can also be synonymous with click() if you don't have a selector specified:

$('.elementClass').click(function() { // code 
});

is synonymous with

$('.elementClass').on('click', function() { // code
});

In the sense that it only add the handler one time to all elements with class elementClass. If you have a new elementClass coming from, for example $('<div class="elementClass" />'), the handler won't be bound on that new element, you need to do:

$('#container').on('click', '.elementClass', function() { // code
});

Assuming #container is .elementClass's ancestor

Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
41

There are a lot of answers, each touching on a few points - hopefully this can give you your answer, with a good explanation of what's what and how to use it.

Using click() is an alias to bind('click' ...). Using bind() takes the DOM as it is when the event listener is being set up and binds the function to each of the matching elements in the DOM. That is to say if you use $('a').click(...) you will bind the function supplied to the click event of every anchor tag in the DOM found when that code runs.

Using live() was the old way in jQuery; it was used to bind events just like bind() does, but it doesn't just bind them to elements in the DOM when the code runs - it also listens to changes in the DOM and will bind events to any future-matched elements as well. This is useful if you're doing DOM manipulation and you need an event to exist on some elements that may get removed/updated/added to the DOM later but don't exist when the DOM is first loaded.

The reason that live() is now depreciated is because it was poorly implemented. In order to use live(), you had to be able to select at least one element in the DOM initially (I believe). It also caused a copy of the function to run to be bound to each element - and if you have 1000 elements, that's a lot of copied functions.

The creation of the on() function was to overcome those problems. It lets you bind a single event listener to an object that will not change in the DOM (so you can't use on() on an element that will be removed/added to the DOM later - bind it to a parent object), and simply apply an element "filter" so that the function is only run when it is bubbled up to an element that matches the selector. This means you have just one function that exists (not a bunch of copies) bound to a single element - a much better approach to adding "live" events in the DOM.

... and that is what the differences are, and why each function exists and why live() is depreciated.

Eli Sand
  • 1,032
  • 7
  • 11
  • 3
    While this question was about .click() vs .on() this is an excellent description of the difference between .live() and .on() which can be difficult to understand the first time someone tries to replace an existing .live() with .on() and has problems getting it to work. It gives a much better explanation than than the one in the official docs. Bonus points for explaining the flaws in .live(). – Night Owl Mar 14 '14 at 06:58
19
  • $("a").live() --> It will apply to all <a>, even if it is created after this is called.
  • $("a").click() --> It will only apply to all <a> before this is called. (This is a shortcut of bind(), and on() in 1.7)
  • $("a").on() --> Provides all functionality required for attaching event handlers. (Newest in jQuery 1.7)

Quotes:

As of jQuery 1.7, the .live() method is deprecated. Use .on() to attach event handlers. Users of older versions of jQuery should use .delegate() in preference to .live().
This method provides a means to attach delegated event handlers to the document element of a page, which simplifies the use of event handlers when content is dynamically added to a page. See the discussion of direct versus delegated events in the .on() method for more information.

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. As of jQuery 1.7, the .on() method provides all functionality required for attaching event handlers.

For earlier versions, the .bind() method is used for attaching an event handler directly to elements.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
7

click() is a shortcut to the non delegation method of on(). So:

$(".close-box").click() === $(".close-box").on('click')

To delegate events with on(), ie. in dynamic created objects you can do:

$(document).on('click', '.close-box') // Same as $('.close-box').live()

But, on() introduces delegation in any static element, not just document as live() does, so:

$("#closestStaticElement").on('click', '.close-box')
elclanrs
  • 92,861
  • 21
  • 134
  • 171
4

You should read up on the difference between live and bind.

In a nutshell, live uses event delegation, allowing you to bind to elements that exist now and in the future.

In contrast, handlers attached via bind (and its shortcuts, like click) attach handlers directly to the DOM elements matching the selector, and therefore are only bound to elements that exist now.

A consequence of live's flexibility is decreased performance, so only use it when you need the functionality it provides.

josh3736
  • 139,160
  • 33
  • 216
  • 263
3

$el.click(fn) is a shortcut for $el.on('click', fn)

See http://api.jquery.com/click/ and http://api.jquery.com/on/ for more info.

Casey Foster
  • 5,982
  • 2
  • 27
  • 27
2

When you need to bind some event handlers to dynamically added elements you have to use live (deprecated) or on make the it working. Simply $('element').click(...); won't work on any dynamically added element in to the DOM.

More on The Difference Between jQuery’s .bind(), .live(), and .delegate().

The Alpha
  • 143,660
  • 29
  • 287
  • 307
1

$.click() is merely a shortcut for either bind or on. From jQuery docs:

In the first two variations, this method is a shortcut for .bind("click", handler), as well as for .on("click", handler) as of jQuery 1.7. In the third variation, when .click() is called without arguments, it is a shortcut for .trigger("click").

Checksum
  • 3,220
  • 3
  • 23
  • 24
1

The .on() method attaches event handlers to the currently selected set of elements in the jQuery object. The click() method binds an event handler to the "click" JavaScript event, or triggers that event on an element.

In the plain .click(... if the target of the selector changes on the fly (e.g via some ajax response) then you'd need to assign the behavior again.

The .on(... is very new (jQuery 1.7) and it can cover the live scenario using delegated events which is a faster way to attach behavior anyway.

ZeroSkittles
  • 164
  • 1
  • 3
  • 16
1

In on method, event handler is attached to the parent element instead of target.

example: $(document).on("click", ".className", function(){});

In above example, click event handler is attached to document. And it uses event bubbling to know whether someone clicked on the target element.

Rajesh
  • 10,318
  • 16
  • 44
  • 64
Sushil Kumar
  • 1,401
  • 2
  • 14
  • 27