Javascript DOM HTML Document getElementsByTagName() Method get first returned element

Introduction

Change the HTML content of the first <p> element (index 0) in the document:

document.getElementsByTagName("P")[0].innerHTML = "Hello World!";

Click the button to change the text of this paragraph.

View in separate window

<!DOCTYPE html>
<html>
<body>
<p>This is also a paragraph.</p>

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

<script>
function myFunction() {/*from  www.j av  a 2s  .c o  m*/
  document.getElementsByTagName("P")[0].innerHTML = "Hello World!";
}
</script>

</body>
</html>



PreviousNext

Related