QP School
Select elements by tag 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 tag name in JavaScript (/showthread.php?tid=4078)



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

The HTML part between the <body></body> tags looks like this:

Code:
<!-- Get elements by tag name -->

<div id="container">
  <a href="https://nodejs.org/">Node.js</a>
  <a href="https://openjsf.org/">OpenJS Foundation</a>
  <a href="https://github.com/tc39">Ecma TC39</a>
</div>

The JavaScript part looks like this:

Code:
let links = document.getElementsByTagName('a');

document.write("<br>");
document.write('Link count: ', links.length);
document.write("<br>");

// Show the href of links

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