tags:

views:

48

answers:

2

Hello,

is there any way I can monitor the entire page (using jQuery 1.3.2) and add a tooltip plugin function to newly created links?

On document.load(), I add a tooltip like this:

$('a').tooltip();

How can I do the same for elements which are added by AJAX? Something using live() perhaps?

Thank you!

A: 

You're looking for the livequery plugin.

However, it's an ugly hack and should be avoided where possible.
In particular, it will not catch any DOM modifications that weren't made using jQuery.

SLaks
Thanks, that was the perfect solution :-)Unfortunately, it rose browser memory usage by over 100%
Rohan
SLaks - With all due respect, your solution would work, but I sure wouldn't take that approach unless absolutely necessary.
patrick dw
Stop recommending liveQuery -- it's antiquated and somewhat redundant considering that jQuery now supports event delegation.
J-P
@J-P: Wrong. Since he wants to run when the elements are created, there is no other alternative. (Unless he controls all of the AJAX call sites) I'm well that livequery is an ugly hack, but it's necessary here.
SLaks
Yeah - a word of warning about live query overhead may be advisable here!
redsquare
@SLaks, It'd be better to suggest changing the plugin so that it uses event delegation.
J-P
That's a third-party plugin.
SLaks
@J-P - SLaks is right, in that `delegate()` is only useful for events, not for plugins that need to run. Still, it is not clear that OP doesn't control the AJAX code. As such, `livequery` should (in my opinion) be a last resort solution. :o)
patrick dw
+1  A: 

When you create your new element using AJAX, run the .tooltip() on them inside the same callback.

success: function() {
    var newelement = $(someNewElement);
    newelement.tooltip();
    newelement.appendTo('body');
}

EDIT: If you can't control the code that is inserting the elements, @SLaks' answer may be a good alternative.

patrick dw