This is such a simple thing, or so I thought. But I just don't get why this is happening...
jQuery's .submit()
method. Seems easy enough – pass it a function and that serves as the listener. Don't pass it anything and it calls .trigger('submit')
. But it's been a huge headache for me over the past 24 hours.
Basically what I'm trying to do is build a simple validation script. The user clicks the submit button on a form, a function runs to validate the inputs, and if all is good in the world, the form submits (the default way, no fancy ajax in this scenario). The issue comes in when trying to call .submit
on either the jQuery object or just the plain 'ole HTML DOM element object. I've learned that jQuery kills the native .submit
on HTMLFormElement, so it makes sense why that doesn't work (I guess, but that seems dumb). But... why wouldn't jQuery's .trigger('submit')
(or .submit()
for short) work?
Example (or take a look at the Fiddle):
HTML:
<form class="comment-form" action="#" method="get">
<input type="submit" id="submit" name="submit" value="submit"/>
</form>
JavaScript:
$('.comment-form').on('click', '#submit', function(e) {
var form = e.delegateTarget;
e.preventDefault();
// For the purpose of demonstration, just setting it true
if (true) {
form.submit(); // object is not a function - this makes sense since jQuery is overriding this
$(form).submit(); // nothin at all :(
$(form).trigger('submit'); // nope, nada
}
});
I did find a workaround by calling the form using .call()
on the HTMLFormElement prototype, but why should I have to do this?
Why? Why? Why?