My HTML has a some anchor tags with a data attribute as below:
<a href="#" class="color" data-colorValue="FF0000">Red</a>
<a href="#" class="color" data-colorValue="0000FF">Blue</a>
<a href="#" class="color" data-colorValue="00FF00">Green</a>
My JavaScript code needs to add a click event listener to each anchor tag. When the event is triggered, I need to get the color value. My code is below.
const colors = document.querySelectorAll('.color');
for(let i = 0; i < colors.length; i++){
colors[i].addEventListener('click', () => {
console.log('test to see if click is working');
console.log(this.dataset.colorValue);
});
}
I'm getting the error "Cannot read property 'colorValue' of undefined"
dataset? That's what's failing. The definition is not shown here.