Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find javascript that is changing DOM element

Are there any techniques I can use to find what javascript is altering an HTML element? I am having some trouble finding how a particular element is getting an inline style of display:none added on load. I know I will find the script that does this eventually, but I want that process to be easier.

My ideal solution would be some way of breaking javascript execution as soon as a DOM element is modified. I am aware of Chrome's dev tools ability to right click an element and select Break On > Attribute Modifications. However, this is happening sometime during page load, so it'd be really nice if I could insert some script before all other script declarations that says 'watch for an element with class XYZ' and break JS execution on element modification. Then, JS execution would either break where I can see the JS that modified the element, or perhaps that could be found by looking at the call stack, but either way, I would be able to see the script that triggered the break to happen. I have found some answers that tell me how to do that using Chrome dev tools / Firebug, but like I said, this question is about the programmatic approach.

like image 977
p e p Avatar asked Jul 25 '14 20:07

p e p


People also ask

How do you detect change in DOM?

Show activity on this post. observeDOMChange(document. querySelector('#dom-changes-here')) . subscribe(val => log('DOM-change detected'));

How do I know if my element is still in DOM?

Use the getElementsByTagName to Check the Existence of an Element in DOM. The function getElementsByTagName() can return all elements with the specified tagName in DOM . The return of the function can be one or more elements or null if no element is found.

Can JavaScript modify DOM?

The HTML DOM allows JavaScript to change the content of HTML elements.

What is a method to find the element you want to manipulate in the DOM?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object.


2 Answers

Right click on DOM element > Break on > Attributes Modifications

Via #3 in https://elijahmanor.com/blog/7-chrome-tips-developers-designers-may-not-know

like image 165
Nighto Avatar answered Sep 27 '22 23:09

Nighto


you can use :-

document.documentElement.addEventListener('DOMAttrModified', function(e){   if (e.attrName === 'style') {     console.log('prevValue: ' + e.prevValue, 'newValue: ' + e.newValue);   } }, false); 

Have a look at this :-

Event detect when css property changed using Jquery

like image 37
Alok Avatar answered Sep 27 '22 22:09

Alok