0

In this code:

<header class="alg">Some text</header>
<script>
var header = document.getElementsByClassName("alg");
header.style.color = 'red';
</script>

after run it. I got from log:

TypeError: header_m.style is undefined

What do I do wrong?

edem
  • 3,222
  • 3
  • 19
  • 45

1 Answers1

4

getElementsByClassName returns multiple elements.

Therefore, you are accessing it improperly. What you want, in this case, is:

 header[0].style.color = 'red';
  //    ^ [0] will get the first element with the class, 
  // which is in this case your is header element. [1] would get the second, etc.

jsFiddle.

dsgriffin
  • 66,495
  • 17
  • 137
  • 137