Javascript DOM HTML Element querySelector() Method get first <p> in <div>

Introduction

Change the text of the first <p> element in a <div> element:

Click the button to change the text of the first p element in div.

View in separate window

<!DOCTYPE html>
<html>
<head>
<style>
#myDIV {/*  w  w w .j  a  v  a  2  s . c o m*/
  border: 1px solid black;
}
</style>
</head>
<body>

<div id="myDIV">
  <p>This is a p element in div.</p>
  <p>This is also a p element in div.</p>
</div>
<button onclick="myFunction()">Test</button>
<script>
function myFunction() {
  var x = document.getElementById("myDIV");
  x.querySelector("p").innerHTML = "Hello World!";
}
</script>

</body>
</html>



PreviousNext

Related