views:

93

answers:

1

I have a function that binds to a tr tag to provide a mouseover effect like this:

$(".grid tr").bind("mouseenter", function () { $(this).addClass("hover"); }).bind("mouseleave", function () { $(this).removeClass("hover"); });

The problem is the grid is loaded via ajax when paging occurs, or filtering etc. This causes the grid to be completely replaced and all the event bindings to fail. Is there a way to bind to an event that automatically attaches to matching elements even when the DOM changes?

Thanks!

+4  A: 

$.live is what you want:

$(".grid tr").live("mouseenter", function () { $(this).addClass("hover"); }).bind("mouseleave", function () { $(this).removeClass("hover"); });
Matthew Flaschen
Boom goes the dynamite
Pierreten
While live() will fix the problem, it's very inefficient. It's better to hook into the success event of the ajax call and update the applicable dom nodes with the new event
Ben Rowe