I’m very new to JS, I’m trying to link two collections. I’m trying to make it so if an HTML element is a certain color, the parent HTML element will be clicked. If anyone could give any advice it would be greatly appreciated 🙂
const heart = document.querySelectorAll(".col > div > div:nth-child(2) > div:nth-child(1) > div:nth-child(1) > div:nth-child(1)");
const faheart = document.querySelectorAll(".fa-heart");
faheart.forEach(function(y, i) {
const m = y.getAttribute("color");
heart.forEach(function(z, i) {
if (m != "#E3170A") {
setTimeout(() => {
z.click();
}, i * 1000)
};
});
});
I tried mapping and using getters, I’m quite new so I couldn’t figure anything out unfortunately. What I’m currently getting is one of the elements just logging the other 24 times.
2
Answers
You don’t need nested loops. Check the colors and access the parent: