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 trial

JavaScript DOM Scripting By Example Adding and Removing Names RSVP Checkbox

classList 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
Steven Parker
231,248 Points

That 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);

Thanks, Steven.

James Crosslin
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
James Crosslin
Full Stack JavaScript Techdegree Graduate 16,882 Points

I 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);
});