fico7489
fico7489

Reputation: 8560

Jquery: how to detect when some element is added to dom?

I want something like:

$('.pac-container').whenAddToDom(function(){
    console.log('element with class pac-container added to DOM');
    //I want to use this added element $(this).doSomethingWithThis();
});

Upvotes: 2

Views: 7474

Answers (2)

fico7489
fico7489

Reputation: 8560

This is solution to my problem:

$(document).bind('DOMNodeInserted DOMNodeRemoved', function(element){
    if($(this).hasClass('pac-container')){
        console.log(element.target);
    }

});

Upvotes: 1

Roysh
Roysh

Reputation: 1550

I would wrap the elements that will be appended in a container and do something like this:

$("html").bind("DOMNodeInserted",function(){
    console.log('element with class '+$("#container > *").attr('class') +' added to DOM');
});

Here's a fiddle http://jsfiddle.net/PgAJT/295/

Upvotes: 4

Related Questions