Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 505
Michel Ayres Avatar asked Apr 19 '12 14:04

Michel Ayres


1 Answers

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')".

like image 64
Armatus Avatar answered Sep 29 '22 11:09

Armatus