Search code examples
jqueryselectorlive

JQuery 'live' for selector?


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?


Solution

  • 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') });