Skip to content Skip to sidebar Skip to footer

Js Submit Not The Same As Html Submit

I have a form that requires a Submit, now for design reasons I have had to relocate the code for the submit of these forms and use JS to actually submit them. This has been workig

Solution 1:

Use the jQuery submit method to trigger event handlers attached with jQuery:

$(document.details).submit(); //shorthand for .trigger('submit')

Demo

This will submit the form in the same fashion, but also trigger the submit event handlers bound to that element with jQuery before doing so.


The issue was that the native HTMLFormElement.submit() method is not warranted to trigger event handlers bound with jQuery (or any event handler at all):

The form's onsubmit event handler (for example, onsubmit="return false;") will not be triggered when invoking this method from Gecko-based applications. In general, it is not guaranteed to be invoked by HTML user agents.

In other hand, jQuery's .submit()/.trigger('submit') will always¹ execute the event handlers attached through jQuery to that element.


¹ Except if event.stopImmediatePropagation() was called previously for the given event but that's a completely different topic.

Post a Comment for "Js Submit Not The Same As Html Submit"