Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MutationObserver observe jquery append?

The following code works on page load but it is never fires when I insert an element (div) to the DOM with jquery.append()

Is there any way to catch with MutationObserver when an element has been added?

here is my code

MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
var observer = new MutationObserver(function(mutations, observer){
     mutations.forEach(function(mutation) {
          if($(mutation.target).length > 0){
               if($(mutation.target).prop('tagName').toLowerCase() === 'img'){
                    console.log('IMG ADDED', $(mutation.target));   
               }
           }   
     });        
});
observer.observe(document,{subtree:true, attributes:true, characterData:true});
like image 937
Eric Avatar asked Aug 31 '25 16:08

Eric


1 Answers

OK I figured it out... Thought I would post to save someone the headache..

I needed childList:true as well

observer.observe(document,{childList:true, subtree:true, attributes:true, characterData:true});
like image 75
Eric Avatar answered Sep 08 '25 01:09

Eric