QP School

Full Version: Select elements by tag 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:
<!-- 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);
  }