Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialRyan Soeder
11,056 PointsclassList toggling
In place of the if...else statement, I used:
listItem.classList.toggle('responded');
Is this an appropriate alternate solution or are there issues with this that I should be aware of?
2 Answers
Steven Parker
231,269 PointsThat makes the result dependent on the current class value instead of the state of the checkbox.
A variant that would operate like the original code would be:
listItem.classList.toggle('responded', checked);
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,882 PointsI absolutely agree with this class toggling and being less verbose if you want to. Mine is way less wordy than Guil wanted, but if you're at that level, you need to practice your skills. Here's mine:
ul.addEventListener("change", (e) => {
const li = e.target.parentNode.parentNode;
li.classList.toggle("responded", e.target.checked);
});
Ryan Soeder
11,056 PointsRyan Soeder
11,056 PointsThanks, Steven.