Javascript DOM HTML HTMLCollection item() Method get first <p> element

Introduction

Change the HTML content of the first <p> element:

document.getElementsByTagName("P").item(0).innerHTML = "Paragraph changed";

Click the button to change the HTML content of the first P element:

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>A P element.</p>
<p>Another P element.</p>
<p>A third P element.</p>
<button onclick="myFunction()">Change Content</button>

<script>
function myFunction() {//from w  w  w .j  a  va  2 s.c  o  m
  document.getElementsByTagName("P")[0].innerHTML = "Paragraph changed";
}
</script>

</body>
</html>



PreviousNext

Related