I change some methods from $(selector).live('click', function(){}); to $(selector).on('click', function(){}); .
At first it seems to work exactly the same. But when I insert new html on the DOM, the new elements inserted with the same selector, they aren’t catch by $(selector).on('click', function(){}); and before they were catch by the live() method.
why? I’m missing something?
.ontakes a different set of parameters compared to.live. You can’t just replace one with the other(and.$(selector).on.livedoesn’t make sense)The correct syntax is:
This will bind to all elements that match
selectorno matter when they are added to the DOM.Note:
Instead of
document, you can use a parent of the element(s), as long as that parent stays in the DOM.For example:
Then you can do:
That click event will fire for all
<p>tags added to#myDiv.Note:
is the same as doing
(jQuery 1.7.1 suggests using
.oninstead of.bind)