Michel Ayres
Michel Ayres

Reputation: 5985

What's the difference between trigger('click') and click() on jQuery

I'm looking for the difference in performance between those two, I could not found in SSE no good answer about this topic.

Some examples would be of great help.

Upvotes: 7

Views: 5417

Answers (1)

Armatus
Armatus

Reputation: 2191

If you look at the jQuery code you can see that all click() does is execute trigger('click'):

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

// Handle event binding
jQuery.fn[ name ] = function( data, fn ) {
    if ( fn == null ) {
        fn = data;
        data = null;
    }

    return arguments.length > 0 ?
        this.on( name, null, data, fn ) :
        this.trigger( name );
};

Note this:

    return arguments.length > 0 ?
        this.on( name, null, data, fn ) :
        this.trigger( name );

In other words, "If no arguments are passed to click, execute trigger('click')".

Upvotes: 9

Related Questions