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 trialninyo
15,367 Pointson()
So basically if the intent for clicking is to append elements dynamically to a parent container, you should always use the on() method? could we just use on() for all clicking or is there a downfall to that? Thanks!
1 Answer
Dave McFarland
Treehouse TeacherThere's no real downside to using .on()
instead of .click()
. In fact, .click()
is just an alias for .on("click")
. In other words,
$('button').click(function() {
alert('ouch!');
});
is the same as
$('button').on('click',function() {
alert('ouch!');
});
The only benefit of .click()
is that it saves you a small amount of typing. The big benefit of .on()
is that it lets you delegate events, so that you can apply event handlers to elements that are added to the page after the page has loaded -- that's the case in the video. Andrew Chalkley uses .on()
applied to the controls
list to listen for any clicks on list items within that element. So when a new color is added to the drawing application (after the page has loaded) you can then click that new color to select it.