Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I break with debugger on all changes to a DOM element?

Tags:

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.

like image 361
Adam A Avatar asked Jun 10 '10 14:06

Adam A


People also ask

How do you break DOM?

A DOM Mutation Breakpoint pauses the code when the DOM node on which you have set the breakpoint is modified. You set a DOM Mutation Breakpoint in the Page Inspector. Navigate to the DOM node in which you are interested and use the context menu to set the breakpoint.

What happens when the DOM changes?

When you update the DOM, the reflow and repaint happen. Every time the DOM changes, the browser needs to recalculate the CSS, do a layout and repaint the web page. React doesn't really do anything new. It's just a strategic move.


2 Answers

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.

like image 144
thegeko Avatar answered Sep 21 '22 10:09

thegeko


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); 
like image 28
James Avatar answered Sep 21 '22 10:09

James