Javascript DOM HTML Element querySelector() Method by id value

Introduction

Change the text of an element with id="demo" in a <div> element:

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

View in separate window

<!DOCTYPE html>
<html>
<body>

<div id="myDIV">
  <p id="demo">This is a p element with id="demo" in a div.</p>
</div>//from   w  w w . j  a v  a  2  s  .c o  m
<button onclick="myFunction()">Test</button>

<script>
function myFunction() {
  var x = document.getElementById("myDIV");
  x.querySelector("#demo").innerHTML = "Hello World!";
}
</script>

</body>
</html>



PreviousNext

Related