views:

20

answers:

1

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  A: 

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);
J-P
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
Click on the "stack" tab -- the functions used to modify the DOM element should be listed there.
J-P
Oh wow, you're right! I dunno how I missed it the first time. Thanks a ton!!!
Adam A