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
Farnoosh M
10,921 PointsaddEventListener
Hi!
In this video we added the event to the button element through:
element.onclick = function_name;
What's the difference between this approach and adding the event with:
element.addEventListener('click', 'function_name', false);
And which one is better? Thanks
1 Answer
Lee Ravenberg
10,019 PointsBoth ways are good. You are just programming against the DOM API in a different way. A few years ago, the implementations of the DOM differed a lot in browsers. I think Internet Explored had a different way of adding an .onclick listener.
For a more in-depth discussion around this I refer you to: http://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick
Farnoosh M
10,921 PointsFarnoosh M
10,921 PointsThanks Lee :)