javascript – What are the differences between doing trigger("click") and click()?

Question:

In a web application, I have found the following code (simplified):

$("#miInput").trigger("click");
$("#miInput").click();

Is there a difference between those two lines? Is it better to use one over the other? (and because)

Answer:

According to the jQuery code , .click() is part of event aliases, which call .on .on() or .trigger() depending on whether or not there are arguments, respectively:

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 );
    };
} );

That said, the only (minor) difference would be to go through one less function using .trigger("click") .

Scroll to Top