Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select elements by tag name in JavaScript
#1
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);
  }
Also follow me on Youtube for videos about video games:
https://www.youtube.com/channel/UCxfkGVU...2mQ/videos
Reply


Messages In This Thread
Select elements by tag name in JavaScript - by Qomplainerz - 04-04-2023, 07:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Add elements at the beginning of an array in JavaScript Qomplainerz 0 354 04-04-2023, 07:37 AM
Last Post: Qomplainerz
  Add elements at the end of an array in JavaScript Qomplainerz 0 316 04-04-2023, 07:37 AM
Last Post: Qomplainerz
  Select elements by name in JavaScript Qomplainerz 0 323 04-04-2023, 07:36 AM
Last Post: Qomplainerz
  Select elements by class name in JavaScript Qomplainerz 0 352 04-04-2023, 07:33 AM
Last Post: Qomplainerz
  Select elements by ID in JavaScript Qomplainerz 0 345 04-04-2023, 07:23 AM
Last Post: Qomplainerz
  Select elements by CSS selector in JavaScript Qomplainerz 0 341 04-04-2023, 07:18 AM
Last Post: Qomplainerz

Forum Jump:


Users browsing this thread: 2 Guest(s)