0

css:

p {
  color: red;
}

html:

<p id='h1'>asd</p>


<script>
var h1 = document.getElementById('h1');
</script>

why do I get an empty string when I type "h1.style.color" in the console?

b4l4g3
  • 61
  • 2
  • 3
  • 1
    Because you haven't set the color property of the style object. Notice, that inline styles are totally separated from cascaded stylesheets. https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle – Teemu Feb 07 '18 at 10:35
  • This is what you are after: https://stackoverflow.com/a/25991851/3233388 – Adelin Feb 07 '18 at 10:37
  • It's empty because you are trying to retrieve this info from CSS, the style.color reads the inline css directive. You can add inline style

    asd

    – AlexCode Feb 07 '18 at 10:42
  • yes, but I want to set the CSS, not the inline css, is there an alternative to style.color? edit: so getComputedStyle, ty Adelin – b4l4g3 Feb 07 '18 at 10:48
  • You can use JQuery if you want to access styles from within CSS. – AlexCode Feb 07 '18 at 10:50

0 Answers0