I have using .on()
in jQuery 1.7 and wondered whether it is possible to attach multiple selectors at a time for elements that have been injected onto a page. Previously, I was using live()
- but it's obvious why I want to move given performance improvements.
Can you use .on()
in the manner like:
$(document).on('click', '#selector1, #selector2, .class1', function () {
//stuff
});
And are there any benefits lost in attaching to document ?
?
it's possible and "this" is the clicked selector, not the document.
you better off attaching to the closest parent element of your selector. When you click on '#selector1', the event bubble up to the event handler element, here: document.
The more layers, the more actions. Moreover, if between selector1 and document there is another click event handler, it can be intercepted with event.stopPropagation();
, and never reach the "document" event handler.
you can check the rogue event "interception" in this fiddle.