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 trialHowie Yeo
Courses Plus Student 3,639 PointsWhy is .onclick() = "click" when using addEventListener?
In the video, he changed to using EventTarget.addEventListener() to replace .onclick but I'm not sure why is the string version of .onclick is define as "click" in the event instead of "onclick" ?
addButton.addEventListener("click", addTask);
addButton.addEventListener("click", ajaxRequest);
2 Answers
akak
29,445 PointsThat's just the syntax of addEventListener() method. It uses "click" instead of "onclick".
http://www.w3schools.com/jsref/met_document_addeventlistener.asp Look in the event section. It says: "Note: Do not use the "on" prefix. For example, use "click" instead of "onclick"." Previously Andrew was using: http://www.w3schools.com/tags/ev_onclick.asp. Those are two different things.
Jarrod Brooks
5,071 Points'click'
is an event type, which is essentially a message.
onclick
is a global event handler that listens for the 'click'
event.
addEventListener('click')
is a a method to register a new listener for any event type
, in this case'click'
.
So basically, 'onclick'
is not an event type in this context. 'click'
is the event type that onclick
listens to.
Howie Yeo
Courses Plus Student 3,639 PointsHowie Yeo
Courses Plus Student 3,639 PointsThanks! Didnt catch that one ;)