Adding Class To Forloop Divs

Hi, I have a calendar, and I'm trying to add a class to the div/day that is clicked to change the background color, but nothing happens when I click any of the Forloop divs. The class is not being added for what ever reason and there is no error in the console. I'm not sure what to do, here is the code related to my issue. Please help 😦
2 Replies
13eck
13eck•3y ago
Your clickedEntryDate() isn't passing the event param, so it's undefined.
const clickedEntryDate = (event) => {
event.target.classList.add('date-clicked');
}
const clickedEntryDate = (event) => {
event.target.classList.add('date-clicked');
}
Also, instead of adding a click listener for each element you should look into using event delegation. That's where you have one listener on the parent element and depending on what was clicked will determine what function is run. https://gomakethings.com/listening-for-events-on-multiple-elements-using-javascript-event-delegation/
Ævie
ÆvieOP•3y ago
could you explain this in more detail? I don't think I'm understanding, I tried the code you posted but that didn't work either okay so i found out that its just not registering the 'click' in the event listener
Want results from more Discord servers?
Add your server