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


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

Forum Jump:


Users browsing this thread: 2 Guest(s)