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

Anton Paluykh
Anton Paluykh
8,994 Points

'change' as an argument in addEventListener()

What does 'change' mean in this code. I have tried to find any info on MDN, but... Originally I have put 'click' instead of 'change' and it works the same way.

ul.addEventListener('change', (event) => {
    const checkBox = event.target;
    const checked = checkBox.checked;
    const listItem = event.target.parentNode.parentNode;
    if( checked ) {
        listItem.classList.toggle('responded');
    } else {
        listItem.classList.toggle('responded');
    }
})

1 Answer

Gergely Bocz
Gergely Bocz
14,244 Points

Hi Anton!

Change is a kind of event, just like click, mouseover, etc. Generally speaking change is preferably used in the case of inputs as inputs can be of many kind. For example change reacts to the change in text aswell and not only when a checkbox is clicked.

There is another reason why Guil used change in this scenario: Event bubbling. Event bubbling means that whenever an event happens on an element, that event "bubbles" upwards to the element's parent and then to that parent's parent, so all way up through the element's ancestors.

You can read more about event bubbling here: https://javascript.info/bubbling-and-capturing

Hope I could help you!

Good luck!