jQuery selector not running inside function called by ajaxcomplete

I need to replace some text in an element that is created dynamically.

I tried:

$(document).ajaxComplete(function() {
       console.log('I started');
       $(".classname:contains('foo')").html('bar');
       console.log('I finished');
 })

The function starts and finishes just when I want it to, but the selection and replacement never happens.

If I just run the replacement in the console, after the element exists, it works fine:

> $(".classname:contains('foo')").html('bar')
> [<label class=​"classname" for=​"element">bar​</label>​]

As does just running the selection and replace constantly with:

var interval = setInterval(function() {$(".classname:contains('foo')").html('bar')})

How can I get my jQuery to run only when I need it to?


Source: jquery

Leave a Reply