Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find the JavaScript that is setting the inline style of an element?

I'm wondering of there is any way to find out where an inline CSS style came from. As you can see in the picture below, I have an element with an inline style that was generated using JavaScript. Sometimes my code seems to break and put the width to 0px, rendering the div invisible.

screenshot

I've looked through all the JS files, but can't seem to find the error.

Is there a way to find the right file and line, just like dev tools does for css files?

like image 646
PieBie Avatar asked Jun 13 '14 08:06

PieBie


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.

What is inline style in JavaScript?

Inline styles are applied directly to the specific HTML element using the style attribute. In JavaScript the style property is used to get or set the inline style of an element. The following example will set the color and font properties of an element with id="intro" .

Where is inline styling specified?

Inline Style Syntax The style attribute is just like any other HTML attribute. It goes inside the element's beginning tag, right after the tag name.

How do you do styling in JavaScript?

There are two other ways to set style using JavaScript. The first is to use the setAttribute() method. The second is by adding a class using the add() method of the classList property. The class you add can then be handled using external CSS styling.

How do I get the inline style of an element?

Like most other attributes, you can get and set the inline style of an element in the DOM with the style property. However, unlike most other attributes, the style property returns an object containing a list of CSS style properties. You need to sign up for Treehouse in order to download course files.

How to get all styles applied to an element using CSS?

To get all styles applied to an element, you should use the window.getComputedStyle () method. Use the properties of element.style object to set the inline CSS properties for the HTML element. Was this tutorial helpful ?

How to modify the inline style of a CSS text?

Or you can use the setAttribute () method: Once setting the inline style, you can modify one or more CSS properties: If you do not want to completely overwrite the existing CSS properties, you can concatenate the new CSS property to the cssText as follows: In this case, the += operator appends the new style string to the existing one.

How do I use the style property in CSS?

The style property returns the read-only CSSStyleDeclaration object that contains a list of CSS properties. For example, to set the color of an element to red, you use the following code: If the CSS property contains hyphens ( -) for example -webkit-text-stroke you can use the array-like notation ( []) to access the property:


1 Answers

Since you are using Chrome:

  1. Right click on the element in the page and Inspect Element
  2. Right click on the DOM inspector view of the element and Break on… → Attributes Modifications

When the inline style of the element is modified with JS, the debugger will trigger as if it had hit a breakpoint.

This will show you the relevant line of JS and give you a stack so you can figure out where that line was called from.

like image 130
Quentin Avatar answered Sep 18 '22 13:09

Quentin