Javascript DOM HTML NodeList item() Method update element

Introduction

Change the HTML content of the first <p> element inside a <div> element:

Click the button to change the HTML content of the first p element inside div.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
div {//  w  ww.  j a v a2 s .  c o  m
  border: 1px solid black;
  margin: 5px;
}
</style>
</head>
<body>

<div id="myDIV">
  <p>First p element in div.</p>
  <p>Another p element in div.</p>
  <p>A third p element in div.</p>
</div>
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var div = document.getElementById("myDIV");
  div.getElementsByTagName("P")[0].innerHTML = "!!Paragraph changed!!";
}
</script>

</body>
</html>



PreviousNext

Related