Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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
218,813 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
218,813 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 :)