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
Akari Nagayoshi
1,210 PointsWhy is it using click() instead of on('click')?
I usually use on('click') instead of click(). Is there any reason for choosing click()? I would like to know the difference between them if there is any. Thank you!
2 Answers
Steven Parker
243,266 Points
In jQuery, .click(handler) is simply a shortcut for .on("click", handler).
There is no difference in functionality. For more info, see the jQuery documentation page for .click.
Now FYI, there's a different three-argument form of .on("click", selector, handler) that Kevin mentioned previously which does have a different functionality. It can be used to create a delegated event handler. A delegated handler can be effective on elements created later if they are descendants of the element the handler is applied to. So another difference besides the additional argument, is that when this form of the method is used, it is applied to a different element.
Another advantage to designated handlers is there is only one, shared by all elements that use it. So where many elements are involved it can yield some memory savings over individual handlers applied to each element.. Again, the jQuery documentation is a good resource.
Steven Parker
243,266 PointsI was a bit uncomfortable with Kevin applying the "Best Answer" designation to my answer without the author's consent. So I deleted my original answer and reposted this one instead.
Kevin Korte
28,149 PointsIt's okay, it's the best choice here. Hopefully this will clear this question up. If Akari wants to reselect (and he should), than we'll let him. But this answer works well here. I'll upvote it so it's at the top, it should be.
Steven Parker
243,266 PointsThank you, Kevin, that's most gracious of you.
Akari Nagayoshi
1,210 PointsHi Steven, Kevin. I previously selected Kevin's answer as Best Answer on my own, so Kevin didn't do anything about it. Just to make sure. Maybe I could wait a bit longer until the whole conversation ended, but I got a few reminder emails from TreeHouse, so I thought I had to choose one very quickly. (This is my first question in the community.)
I know my question was very simple, but I am very glad that Kevin brought up more advanced thing as further reference. I think that is what this community is for. Since Steven summed up all the comments so far in one post, which is very clear to understand, I reselect it as Best Answer. I think it is very good summary for other students too.
Thank you for teaching me how the delegated handler works, Kevin! And thank you for being careful and making all the things very clear, Steven. Both of you helped me a lot! Thank you so much :)
Kevin Korte
28,149 PointsIn light of recent events, and conversations with Steven outside of Team Treehouse, it's now become very clear that my answer was incomplete, and taking this in a direction that Akari didn't ask about, and I wanted to avoid further confusion of myself or anyone else.
Akari Nagayoshi
1,210 PointsThank you Steven and Kevin! I read the official documentation Steven posted, and there were a lot of things that I did not know about delegation. The documentation also made the memory usage Kevin explained very clear. Thank you to both of you again!
Akari Nagayoshi
1,210 PointsAkari Nagayoshi
1,210 PointsThank you for the answers, Steven and Kevin! I chose Kevin's as Best Answer because he solved exactly what I was wondering. Very good to know. Thank you :)