I want to know if a user selected a size before purchasing clothes,
so I attached an event listener to it's parent container, the selection area.
Product card with selector tiles
If I pass in a bound function as an argument to the event listener,
event.target becomes the document object on click.
if(selectionArea) {
selectionArea.addEventListener("click", removeWarningStyles.bind(null, event, sizeTiles,warning));
}
Remove warning styles method:
function removeWarningStyles( event, sizeTiles, warning) {
if(event.target.matches(".size-tile")) { // This becomes the document object. Why?
for(let tile of sizeTiles) {
if(tile.classList.contains("size-tile--not-properly-chosen")) {
tile.classList.toggle("size-tile--not-properly-chosen");
}
}
warning.style.visibility = "hidden";
warning.style.opacity = "0";
}
}
I've even tried to bind event.target and event.currentTarget, but it still doesn't work.
If I write the anonymous function directly into the handler,
it works perfectly. But why?
if(selectionArea) {
selectionArea.addEventListener("click", (event) => {
if(event.target.classList.contains("size-tile")) { //becomes size-tile element correctly
for(let tile of sizeTiles) {
if(tile.classList.contains("size-tile--not-properly-chosen")) {
tile.classList.toggle("size-tile--not-properly-chosen");
}
}
warning.style.visibility = "hidden";
warning.style.opacity = "0";
}
});
}