jQuery - replace .live() method with .on()
According to the jQuery documentation you should refrain from using the live() or delegate() methods because they have been deprecated in favor of the on() method.
The great thing about the live method was that it attached handlers based on selector not only for current, but also for future matching elements. HTML snippets retrieved via Ajax or generated via JS termplates would have the correct handlers attached automatically.
Here's how you replicate this same behavior with the on() method:
The great thing about the live method was that it attached handlers based on selector not only for current, but also for future matching elements. HTML snippets retrieved via Ajax or generated via JS termplates would have the correct handlers attached automatically.
Here's how you replicate this same behavior with the on() method:
$(selector).live(event, handler) // should be replaced by: $(document).on(event, selector, handler) //The event bubbles up to the document element which has the handler attached to it. //This way a handler doesn't have to be attached seperately to every element matching the selector. // you could/should also stop the event bubbling - // at an earlier stage by attaching event handlers further down the document tree: $("#dataTable tbody").on("click", "tr", function(event){ alert($(this).text()); });