views:

41

answers:

2

Hello,

I would like to have the same behaviour 'live' has, but without using events. Is this possible? I need this because I'm using a different javascript file that creates some elements to which I need to append css classes.

The following code should execute once such an element is added to the dom: $(".myClass").addClass("myNewClass");

Is this even possible?

+1  A: 

The liveQuery plugin is capable of this.

http://brandonaaron.net/code/livequery/docs

From the docs: Live Query also has the ability to fire a function (callback) when it matches a new element and another function (callback) for when an element is no longer matched.


EDIT:

This would be a code solution using livequery.

$('.myClass').livequery(function() { $(this).addClass('myNewClass') });
patrick dw
Works perfectly! Thanks
SaphuA
The other option is to do a rebind when the form changes eg on ajax success. Livequery can slow things down a bit.
James Westgate
@James Westgate - What form do you mean? The OP wanted to add a class for elements added to the DOM by another js file that can't be edited. There no event to bind (or re-bind) that to.
patrick dw
@Patrick - Doh! I meant dom.
James Westgate
A: 

Perhaps I don't understand what you are asking, but if you are using jQuery, why can you not use an event driven update?

The liveQuery solution Patrick suggests (which will work just fine) uses events in the background, just like 'live'.

Dave
Because I'm in a weird situation ;) I can't modify the javascript code I'm using and am unable to get events triggered. I do know the classnames it uses for the created elements so figured this would be the best sollution for me.
SaphuA