I have an OnClick event and each img element has a unique numerical identifier defined in "data-index".
The goal is to change css class of both elements when the image or span element are clicked.
Right now I can only toggle the class of either the image or the span element This is like a character select screen.
function selector() {
var getSlots = Array.from(document.querySelectorAll('.slotimage', '.charname'));
for (var i = 0; i < getSlots.length; i++) {
getSlots[i].dataset.index = i;
getSlots[i].onclick = function(e) {
var index = this.dataset.index;
if (this.classList.contains('unselected')) {
this.classList.toggle("unselected");
this.classList.add("selected");
} else {
this.classList.toggle("selected");
this.classList.add("unselected");
}
};
}
}
I have tried using getSlots[i].classlist
but I keep getting an error.
If I don't use "this" then nothing works
I've got ~30 items so I need to toggle the styles of the one I clicked