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

element.addEventListener('change', function () {}) VS. element.onchange What is the difference between the two?

I used both of the methods in getting the value of a select drop down and I can't see a difference between the two. Is there a benefit of using one over the other?

2 Answers

element.onchange = function() {}

This will set the function as the only function to the event.

element.addEventListener('change', function() {})

This will add the function to the event. This is the only way to have multiple event listeners on the same element.

Thanks Michael! You're the man!