Last Updated: February 25, 2016
·
450
· 4051kanobi

.live() is dead <- carry .on()

Capturing events of dynamic content (ajax etc...) used to depend on the .live() event handler. The new and improved handler is .on() which allows you to limit the scope of the "area" you're tracking.

so:
$(document).on('click', '#trackThis', function(){});

plain-speak:
jQuery, within the document, on clicking the element with id trackThis, perform this function.

limits the event handler to the document scope which results in faster event tracking.

3 Responses
Add your response

This is actually

jQuery, on clicking on the document, checks if the target is the element with the trackThis id, and if so, performs this function.

That's why on is good to attach handler to multiple elements, but if you check an id, you'd better attach the handler directly to the unique element.

over 1 year ago ·

This was in regards to dynamic content, specifically content injected with ajax after the DOM is loaded; attaching the handler directly to the id will not work, however, thank you for the clarification, i found that this topic has been exceedingly difficult for front-end developers to grasp

over 1 year ago ·

Liked your title. That is all.

over 1 year ago ·