Using $("#myTable tr").live("click", someCallback); (although now deprecated) it is fairly simple to subscribe to the click event for newly added table rows.
I would be interested in a way which doesn’t only support events but any jQuery commands, something like:
$("myTable tr").live().attr("hello", "world");
which would add the hello=”world” attribute to any newly added table rows. Is there any such thing in jQuery?
Update: Please note. I want to subscribe to the event of an element being added. I do not control the code which adds it. Take dataTables as a example: The dataTables plug-in creates the table rows.
I would say there are two reasons why this doesn’t really exist.
DOMSubtreeModifiedin Chrome and FF, but not in IE and Opera..live/.delegateis already pretty inefficient of a strategy.To help picture 2, imagine that 1 isn’t a problem and we can capture any time an element is added to the DOM. When this happens, we need to evaluate the new element and its children against
"#myTable tr". This will significantly increase the amount of non-native code executing with every DOM change in scope, really slowing things down.The best strategy is to add your desired jQuery changes to the code that would be inserting the
<tr>UPDATE If you have no choice and only have to support Chrome and FF, there is one solution I can think of. Obviously, it will not be fast and you can improve your performance through specificity:
See an example here: http://jsfiddle.net/vCZHQ/
You could theoretically check if the table has changed in IE by doing a timeout:
That said, this will have a slight delay and really drain a laptop’s battery.