QP School

Full Version: Select elements by class name in JavaScript
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The HTML part between the <body></body> tags looks like this:

Code:
<!-- Select elements by class name -->

<div id="container">
  <p class="note">The first note.</p>
  <p class="note">The second note.</p>
  <p class="note">The third note.</p>
</div>

The JavaScript part looks like this:

Code:
// Select elements by class name

let elements = document.getElementsByClassName('note');

for(let i = 0; i < elements.length; i++)
  {
    document.write(elements[i].innerHTML);
  }