0

I would like to change the text color by following javascript, but it not work, please suggest how to fix this problem, many thanks

function myFunction() {
  var x = document.getElementsByClassName("eee").getElementsByClassName("abc")
  x.style.color = "red";
}
<!DOCTYPE html>
<html>

<body>

  <p id="demo" class="eee">
    <p id="ccc" class="abc" style="color: green;">Click the button to change the color of this paragraph. </p>
  </p>

  <button onclick="myFunction()">Try it</button>

</body>

</html>
Ivan86
  • 5,642
  • 2
  • 12
  • 30
Jack Wong
  • 27
  • 6

1 Answers1

0

Try this..

<!DOCTYPE html>
<html>
<body>

<div id="demo" class="eee">
    <p id="ccc" class="abc" style="color: green;">Click the button to change the color of this paragraph.</p>
</div>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
  var x = document.getElementsByClassName("abc")
  for (let i=0; i<x.length; i++) {
    x[i].style.color = "red";
  }
}
</script>

</body>
</html>
Phani
  • 82
  • 7