2

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();
});
fico7489
  • 7,931
  • 7
  • 55
  • 89
  • 2
    Check this question. It may helpful. http://stackoverflow.com/questions/3219758/detect-changes-in-the-dom – suyesh Jun 13 '16 at 12:51
  • 1
    Or this question: [http://stackoverflow.com/questions/2844565/is-there-a-javascript-jquery-dom-change-listener](http://stackoverflow.com/questions/2844565/is-there-a-javascript-jquery-dom-change-listener) – Mikey Jun 13 '16 at 12:52
  • ***Mutation Observers*** is what you are looking for. – Jai Jun 13 '16 at 12:54
  • 1
    you can listen to elements that are being appended `.bind('DOMNodeInserted DOMNodeRemoved')` – Roysh Jun 13 '16 at 13:01

2 Answers2

4

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/

Roysh
  • 1,542
  • 3
  • 16
  • 26
1

This is solution to my problem:

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

});
fico7489
  • 7,931
  • 7
  • 55
  • 89