3

My problem: after load some element via ajax, i bind some on-click function, but when user will load few times that same element, binded action will be repeat (not replace, at least that it looks like). I tried unbind, or click(function(){return false;}); but that complete remove clic action from element...). What is standard solution of that kind of problem?

IProblemFactory
  • 9,551
  • 8
  • 50
  • 66

2 Answers2

6

For most events you can use live() (jQuery 1.3+):

$("td").live("click", function() {
  // do stuff
});

This will bind a click event to <td> elements that come into existence after you run this code as well.

This is a much cleaner solution than trying to bind/unbind and ensure you don't have the same event bound twice to a particular element.

cletus
  • 616,129
  • 168
  • 910
  • 942
  • Thx, +1 and `correct answer`, but a little problem still is: with `not` it suppose to don't work... `$("...").not("sada").live("click", func...`. – IProblemFactory Oct 21 '09 at 17:30
  • That's going to be a little more problematic. What exactly are you trying to do? I can perhaps suggest a way that'll work. – cletus Oct 21 '09 at 17:36
1

if you're using jQuery 1.3.2, you can use $('').live('click', function() {}); to have any elements that match that selector have the action. It keeps the event around even with new elements.

Agent_9191
  • 7,216
  • 5
  • 33
  • 57