JavaScript querySelector() method - type of element with class example

by danielkwood

HTML

<html>
  <head>
    <title>querySelector() method</title>
  </head>
  <body>
    <h1 class="myClass">This is some text</h1>
    <p class="myClass">This is some more text</p>
  </body>
</html>

JavaScript

// get the first element that has the ID "myID" using querySelector("p");
var myPararagraph = document.querySelector("p.myClass");

// Change the innerHTML content
myPararagraph.innerHTML = "This is some new text";