Search code examples
javascriptjqueryeventsdom

disable dom changed event in Javascript?


I use

document.addEventListener("DOMSubtreeModified", function() {
    ...
});

to act whenever the DOM of the document is changed.

The problem is: In that function I modify the DOM myself but I do not want this change to call the DOMSubtreeModified event (causing my code to be run over and over again).

Any chance to prevent that?


Solution

  • I have done the same once using this solution:

    function modifyDOM(obj) {
        obj._muteTrigger = true;
    
        // HERE do your actual dom change
    
        obj._muteTrigger = false;
    }
    
    document.addEventListener("DOMSubtreeModified", function() {
        if(this._muteTrigger) return;
        
        //the other codes
    
        modifyDOM(this);
    });