Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find which JavaScript is changing an element's style?

I have an element that's getting styles applied via JavaScript. I'm not sure exactly where; is there a way to check Firebug to show where the "element.style" is actually coming from?

like image 722
Andy Avatar asked Aug 03 '10 22:08

Andy


People also ask

How do you know where an element style is coming from?

In Chrome, if you right click on an element and "inspect," then view the styles in the "Computed" tab then you should see what styles are affecting the element.

Can JavaScript change the style of an element?

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

What is the JavaScript property to change the style of an HTML element?

style. The style read-only property returns the inline style of an element in the form of a CSSStyleDeclaration object that contains a list of all styles properties for that element with values assigned for the attributes that are defined in the element's inline style attribute.

How do you check if an element has a style?

To check if an element's style contains a specific CSS property, use the style object on the element to access the property and check if it's value is set, e.g. if (element. style. backgroundColor) {} . If the element's style does not contain the property, an empty string is returned.


2 Answers

If you're sure it's being set on the inline style and not as a consequence of a stylesheet rule, you can detect changes using the non-standard Mozilla watch() method:

document.body.style.watch('color', function(name, v0, v1) {     alert(name+': '+v0+'->'+v1); }); document.body.style.color= 'red'; 

You can put debugger; in the watcher function and look up the call stack in Firebug to see where the change was triggered.

like image 115
bobince Avatar answered Oct 07 '22 19:10

bobince


On request from this question:

If you have firefox you can check the "Break on Attribute Change" option in the HTML tab. Just right click the target element and the menu will pop up. After that, resize the window and it will break in the script line where the attribute is changed. use firebug

like image 20
Simon Avatar answered Oct 07 '22 19:10

Simon