0

I have a page where I do:

$(document).ready(function(){
   $("a.notifypop").bind('click', function(){
       // Do stuff
       return false
   });
});

When I replace the content of the page I also replace the anchor that I have bound the click event to. The result is that the click event is no longer bound to the anchor - what to do in that situation? I HAVE to replace the anchor.

  • Jacob
user113716
  • 318,772
  • 63
  • 451
  • 440
jriff
  • 1,947
  • 3
  • 23
  • 33

3 Answers3

2

You can use .delegate instead. .delegate is significantly cheaper then .live And will bind to all all elements matching the selector.

See the documentation on .delegate

$("body").delegate("a.class", "click", function() {
    // do stuff
});

See Nick Craver's Answer as to why .delegate is better

Community
  • 1
  • 1
Raynos
  • 166,823
  • 56
  • 351
  • 396
1

You need to bind with .live, not .bind. .bind only applies to objects that exist at DOM load. Anything you create via JS, jQuery, AJAX, etc. that didn't exist at page load must be bound with .live for functionality.

$("a.notifypop").live('click', function(){
  // Do stuff
  return false;
});
sethvargo
  • 26,739
  • 10
  • 86
  • 156
0

If your new anchor has the same class:

$('a.notifypop').live('click', function(){
});
Jason Benson
  • 3,371
  • 1
  • 19
  • 21