Skip to content Skip to sidebar Skip to footer

JQuery Methods: Difference Between .submit() Vs .trigger('submit')

jQuery allows to trigger a form submission programmatically via either: $('.js-form-class-hook').submit(); $('.js-form-class-hook').trigger('submit'); Note: My understanding is

Solution 1:

There is essentially no different between submit() without an argument, and trigger('submit'), in fact submit() with no arguments will eventually return trigger() anyway.

You can prove this by looking at the jQuery Source:

jQuery.fn.submit()

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

So, if an argument is passed to submit(), .on('submit'... will be called, otherwise, .trigger('submit') will be called.

submit() is pretty much a more human-readable way of calling trigger('submit'). There are no special features for either, which one you choose is personal preference.

Note: The same goes for click(), change() etc..

is "allowing to pass arbitrary data" the only advantage of using .trigger('submit') ?

Unless you consider one less function call an advantage, yes, it is.


Post a Comment for "JQuery Methods: Difference Between .submit() Vs .trigger('submit')"