QP School
Select elements by class name in JavaScript - Printable Version

+- QP School (https://qomplainerzschool.lima-city.de)
+-- Forum: Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=3)
+--- Forum: JavaScript Tutorials (https://qomplainerzschool.lima-city.de/forumdisplay.php?fid=6)
+--- Thread: Select elements by class name in JavaScript (/showthread.php?tid=4077)



Select elements by class name in JavaScript - Qomplainerz - 04-04-2023

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);
  }