0
<script>
    document.getElementsByClassName("blue").text-color = "darkblue";
</script>

I saw this as a practicing section for JavaScript beginners. I am unable to find the error.

<div>
    <span class="blue">This text should be dark blue.</span><br>
</div>

What change must be done to rectify this error?

Mamun
  • 58,653
  • 9
  • 33
  • 46

1 Answers1

2

getElementsByClassName() returns HTMLCollection. You have to specify index to get the element you want. To set the color of the text you have to set the color property on style:

Change document.getElementsByClassName("blue").text-color = "darkblue";

To

document.getElementsByClassName("blue")[0].style.color = "darkblue";

Working Code Example:

document.getElementsByClassName("blue")[0].style.color = "darkblue";
<div>
    <span class="blue">This text should be dark blue.</span><br>
</div>
Mamun
  • 58,653
  • 9
  • 33
  • 46