1

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

Jacob Raccuia
  • 1,666
  • 1
  • 16
  • 25

3 Answers3

1

try

$(document).delegate('.delete_gallery', 'click', function(event) { 
Dakait
  • 2,531
  • 1
  • 25
  • 43
1

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

darronz
  • 903
  • 9
  • 17
  • Can you clarify on this, as this actually fixed it. .delete_gallery was a direct child of .trash, so I assumed it would be better to be more direct. – Jacob Raccuia Feb 28 '13 at 06:45
  • 1
    @Jacob, your `.trash` elements are probably dynamic. Event delegation works by binding events on *non-dynamic* ancestors to your dynamic content. `document` works because it's the root of the DOM tree and, as such, always static. – Frédéric Hamidi Feb 28 '13 at 06:47
  • That would seem to be the case. Thank you! Selecting this answer as it holds the reason why. – Jacob Raccuia Feb 28 '13 at 06:53
0

You still have .bind().

Example

$(".trash").bind('click', function(event) {
Dipesh Parmar
  • 27,090
  • 8
  • 61
  • 90