Javascript DOM HTML Document getElementsByTagName() Method update all paragraph element

Introduction

Change the background color of all <p> elements in the document:

Click the button to change the background color of all p elements in this document.

View in separate window

<!DOCTYPE html>
<html>
<body>

<p>This is a p element</p>

<p>This is also a p element.</p>

<p>This is also a p element</p>

<button onclick="myFunction()">Test</button>

<script>
function myFunction() {//from  w w w .j a  v  a  2 s.c om
  var x = document.getElementsByTagName("P");
  var i;
  for (i = 0; i < x.length; i++) {
    x[i].style.backgroundColor = "red";
  }
}
</script>

</body>
</html>



PreviousNext

Related