The expected behaviour is that when I click any of the rating circles, their color should change to grey for which I have a class defined in my css that I toggle using js code.
The codepen for my solution is this. Using the js code that I have written, whenever I click any of the ratings, the last one turns grey all the time.
This is my current code:
// getting the objects
const submitBtn = document.querySelector('button');
const ratingsParent = document.querySelector('.ratings');
console.log(ratingsParent.children);
// sanity check
submitBtn.addEventListener('click', () => {
console.log('Clicked');
})
for (var r of ratingsParent.children) {
r.addEventListener('click', ()=> {
r.classList.toggle('clicked');
})
}
But when I change r.ClassList.toggle to evt.target.ClassList.toggle, it shows the expected behaviour. Im extremely new to JS and just starting to learn frontend dev so Im unable to figure out the reason for this behaviour.
