-1

I'm working on something where I would need to return an element's style and print it in the HTML page itself. Okay I know how to print it but the problem is how do I get the element. To test if it would print the style I tried

console.log(document.getElementById('something').style.fontSize);

but it returns undefined, even though 'something' does have a set font size.

SirMaxime
  • 154
  • 1
  • 4
  • 9
  • Why do you want to do this? Needing/wanting to "read" the style from an element is often a sign of somehow trying to maintain application state in the style information, and there are usually better ways to approach things. –  Dec 06 '15 at 16:33

1 Answers1

7

You need to get the computed style.

getComputedStyle(document.getElementById('something'), null);

So replace your code with:

console.log(getComputedStyle(document.getElementById('something'), null)
  .getPropertyValue("fontSize"));

See Window.getComputedStyle()

canon
  • 40,609
  • 10
  • 73
  • 97
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252