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

.on vs .click Event Handling

Just a general question... Wouldn't it easier to just use .on for all Event Handling? Just wondering what the benefits of using shorthand are. It seems like getting in the habit of using .on would make this easier to learn rather than having to remember all of the different short hand event handlers.

3 Answers

.on() works on dinamically added elements while .click() doesn't.

Say you have a button to add elements that do something on click, if you use .click() it doesn't work on the newly added elements, if you use .on('click', '.element-added', () => { do something }); it does work.

Thanks Jesus.

Steven Parker
Steven Parker
243,318 Points

If you can remember the event name, you should be able to use the shorthand.

If you use "on", you still need to enter the event name as the first argument. The shorthand is simply the name of the event itself.

.on("click"... :point_left: same as :point_right: .click(...
.on("keyup"... :point_left: same as :point_right: .keyup(...
...etc.

Thanks for your reply, Steve. I understand that aspect of the functions.

Let me rephrase my question, beyond the differences in syntax and need for a parameter, is there really a difference in terms of usage or performance OR does it just really come down to the developer's preference?

I see your point in that using the shorthand would be less typing. I think I'm just looking at it from an association standpoint in that .on may be easier to remember and associate with event handling rather than having to memorize each individual shorthand function and recognize it as an event handler rather than a different method or function.

Hi Jeremy,

I'd say it's really down to developer preference unless you need to do event delegation which Jesus and Kevin talked about. In that case, you have to use the .on() method.

I think these shortcut methods basically exist for less typing. If you need to initiate a click event, it's simply easier to type .click() than .trigger("click")

In terms of performance, it's likely a negligible difference. The shortcut methods are going to be slightly slower.

Here's a link to the alias.js file from the jQuery source: https://github.com/jquery/jquery/blob/master/src/event/alias.js which I think is the relevant code.

lines 10 to 21 of that file:

jQuery.each( ( "blur focus focusin focusout resize scroll click dblclick " +
    "mousedown mouseup mousemove mouseover mouseout mouseenter mouseleave " +
    "change select submit keydown keypress keyup contextmenu" ).split( " " ),
    function( i, name ) {

    // Handle event binding
    jQuery.fn[ name ] = function( data, fn ) {
        return arguments.length > 0 ?
            this.on( name, null, data, fn ) :
            this.trigger( name );
    };
} );

On line 17 there's a ternary operation which checks the number of arguments passed in. If 1 or more arguments were given then the .on method will be called with the arguments passed on through. If no arguments were given then the .trigger method will be called instead.

So when you use the .click method you have the slightly extra overhead of that ternary operation and another function call.

I upvoted and Jesus, and I just want to reiterate what he said. When you have new elements append to the dom, either by ajax, or some sort of pub/sub model (live comment feed, maybe), .click() won't do anything on elements added after the page was loaded. But .on('click', function(){}) will. That is a very big fundamental difference between the two, that can save you a lot of head banging on the wall.

I don't know on performance metrics, but my guess is .click() is probably a tad faster when it's safe to use, but not enough to deter me from not using the .on('click', function(){}) way.

Thanks Kevin. If I could give out two best answers, I'd give you one as well. I'm giving it to Jesus just because he replied first. Thanks again for your response though. It's very insightful.

You're welcome. Jesus deserves the best answer! You made the right choice.