let's say I have my HTML document like
<a href='#' class='class_1'>Content</a>
<a href='#' class='class_2'>Content</a>
<a href='#' class='class_3'>Content</a>
<a href='#' class='class_4'>Content</a>
<a href='#' class='class_5'>Content</a>
Now, I have selected those anchor tags with querySelectorAll and looped through them using forEach.
Inside forEach, after calculating some stuff, I need to apply a style to a specific anchor tag with its class.
anchorList.forEach(link => {})
How can I select that link with a certain class without using another loop?
Since you are already looping through all of the anchor tags using a forEach, you can check for the specific class in the loop itself. Try this code:
var anchorList = document.querySelector("a");
anchorList.forEach(link => {
// calculations...
if(link.className === "class_1"){
link.style.color = "red"; // or anything else
}
// continue calculations...
});
Don't hesitate to comment if something is incorrect or not what you were asking for, I'll update my answer.