jqueryjquery-delegate

Using jQuery delegate() to replace live()


I currently am creating content using jQuery.

Live(), according to the jQuery API Description: Attach an event handler for all elements which match the current selector, now and in the future.. I need this line of code to work in the future.

$(".trash").delegate('.delete_gallery', 'click', function(event) { // does not work $(".delete_gallery").live("click", function(event) { // works

How can I get the delegate to work? I don't want to use a deprecated function.

.on() is not a choice. Hopefully this isn't a duplicate question either, I couldn't find any that addressed this, they all just say how to do it.

Thanks! Jacob


Solution

  • You'd be better delegating the event handler to $(document) rather than $(".trash') as it means you're not relying on it existing.