27

I would really love to be able to see the code that is affecting a specific DOM element.

But I also would really love not to have to look through all my javascript searching for references/selectors that might be causing the issue.

Does anyone have a technique for causing a browser debugger to break on any changes to a specific DOM element? I don't mind it requiring a specific browser or extension to work.

1
  • Please change the accepted answer. The events in the accepted answer are a bit out of date now. @thegeko answer is better.
    – Ifan Iqbal
    Commented Mar 24, 2014 at 20:42

2 Answers 2

35

This is also doable without writing any script in Firebug as well as in Chrome's developer tools (maybe others, did not inspect further).

In Firebug:

  1. Go to HTML tab
  2. Right-click an element you'd like to monitor
  3. Choose "Break On Attribute Change", or "Break On Child Addition Or Removal", or "Break On Element Removal"

In Chrome Developer Tools

  1. Go to Elements tab
  2. Right-click an element you'd like to monitor
  3. Select "Break On ...", then choose "Subtree Modification", or "Attributes Modification", or "Node Removal"

I actually found this out after trying accepted answer of 999, however given code did not work for me. Additionally, Chrome's possibility to monitor events on any DOM subtree seems really nice.

2
  • 2
    Yeah The events in the above answer are a bit out of date now. They are deprecated in favor of MutationObservers (developer.mozilla.org/en-US/docs/DOM/MutationObserver). I will add a note to the other answer. Thanks for your answer!
    – Adam A
    Commented Jan 30, 2013 at 10:35
  • Well it still works for me on Firebug + Firefox 49.0.1, and it's more convenient than the other answer :P Commented Oct 11, 2016 at 15:01
12

Note: The events below were great when the question was asked, but are no longer current. The recommended alternative is MutationObservers, but those are still maturing

MutationObserver on MDN


Try this (in Firefox, with Firebug installed):

function breakOnChange(el) {

    if (!el.addEventListener) return;

    el.addEventListener('DOMAttrModified',
         function(DOMAttrModifiedEvent){debugger}, true);

    el.addEventListener('DOMNodeInserted',
         function(DOMNodeInsertedEvent){debugger}, true);

    el.addEventListener('DOMNodeRemoved',
         function(DOMNodeRemovedEvent){debugger}, true);

}

// Usage:
breakOnChange(someDomNode);
4
  • This is pretty awesome and gets me most of the way there. It doesn't let me find the code that did the changing, though. I don't suppose there's some way to pull the previous call stack out of it, is there?
    – Adam A
    Commented Jun 10, 2010 at 14:58
  • Click on the "stack" tab -- the functions used to modify the DOM element should be listed there.
    – James
    Commented Jun 10, 2010 at 15:05
  • @James Neat trick! Super useful in helping me debug an issue on 1000+ lines of minified JS. Thanks!
    – mVChr
    Commented Mar 18, 2014 at 19:53
  • Here's a shortened version you can paste into the console: ((selector='DOM SELECTOR GOES HERE')=>['DOMAttrModified','DOMNodeInserted','DOMNodeRemoved'].map(ev=>document.querySelector(selector).addEventListener(ev,()=>{debugger})))()
    – jameslk
    Commented Aug 4, 2021 at 2:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.