How to target all elements of a given class name in JavaScript

JavaScript getByClassName only targets one element by default

Workarounds "loops"
Loops can mess up your code I personally don't like them.
To target all elements you will have to iterate through all of them for each element you will have to add a event listener

Example
For instance the are 3 links of a same class and you want to add event listeners on all of them
....
....
....
// JavaScript
const links = document.getByClassName("links")
for(var i = 0; i < links.length; i++)
links[i].addEventListener('click', event => console.log(event.target.get attribute('class')))

Comments

Popular posts from this blog

How to make a html5 breakout game in JavaScript

How to tabulate array key values using console.table

How to set content disposition headers for express nodejs apps

Ways of iterating Object properties in JavaScript

How to evaluate a string on a regular expression rule in JavaScript