hey y’all. So I keep running into this problem with my programs, I’m wondering if anyone can help me understand what’s happening.
in this scenario I have a 5 html elements. I do a querySelectorAll for this group of elements. Then I loop over the returned nodeList. I want to click on a selected node and change its style. What is the proper way to achieve this goal?
<svg>
<svg>
<svg>
<svg>
<svg>
var svg = document.querySelectorAll('svg');
for (var i = 0; i < svg.length; i++) {
svg[i].onclick = () => {
svg[i].style.fill = "black";
};
}
for (var i = 0; i < svg.length; i++) {
svg[i].addEventListener("click", function () {
svg[i].style.fill = "black";
console.log(svg[i]);
// ^^^ svg[i] is undefined
});
}
I’m getting the same thing with the event listener