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 AJAX Basics (retiring) AJAX and APIs Adding jQuery

Wouldn't it be better to use .on() method rather than .click() ?

Wouldn't it be better to use .on() method rather than .click() ?

1 Answer

Steven Parker
Steven Parker
231,236 Points

As mentioned on the documentation page, the .click( handler ) method is simply a shortcut for .on("click", handler ),

Or were you suggesting using one of the other forms of the .on() method?

Nope I was referring to .on("click" method, because correct me if I'm wrong but from what I understood from previous lessons that it is more practical to use .on("click") rather than .click() because we might want to add more triggers in future and .on('click') can use less memory and work for dynamically added elements. Source: https://stackoverflow.com/questions/9122078/difference-between-onclick-vs-click

Steven Parker
Steven Parker
231,236 Points

That guy is suggesting using one of the other forms, specifically to create a designated handler. It's not possible to get the functionality he's referring to using "click" because it requires additional arguments. If you're writing a designated handler you don't have a choice.

But when you only need the standard functionality, then "click" is a shortcut (no difference in function or performance). It makes your code a little bit more concise and easier to read.

In the exemple it's ok to use .click();

There is always many ways to write the code even using the same language. .on() is a must use if you might have a dynamically generated elements. And it's good because the event delegation stuff.

So in this case use whatever you like to use :)