Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Select elements by class name in JavaScript
#1
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);
  }
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 tag name in JavaScript Qomplainerz 0 324 04-04-2023, 07:35 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)