What I am trying is to catch all the form submit events, get the url from the action attribute and use it to send the content of the form to that address using AJAX. So what I need is only one on submit event handler. However I quickly got in trouble as it seems to not be working in IE.
$(document).submit(function(e) {
alert('clicked');
e.preventDefault();
});
This is the code I use for cross-browser testing purpose. It works perfectly in Chrome, Firefox but not in IE. Am I not allowed to set an event listener on document?
The NUMBER ONE error is having ANYTHING with the reserved word submit as ID or NAME in your form. If you plan to call . submit() on the form AND the form has submit as id or name on any form element, then you need to rename that form element, since the form's submit method/handler is shadowed by the name/id attribute.
No, it's not deprecated! Deprecated = Not current. Obsolete = no longer available.
Sometimes the problem is caused by old versions of the Javascript files, cached by your browser and can be fixed by clearing the browser cache. You can use the browser console of your browser for debugging. After the Javascript error is fixed, the submit button will automatically be enabled.
The onsubmit handler is not called, because the form cannot be submitted by any normal means, i.e. the submit event cannot be caused. There is only one submit control, and it is declared as disabled. Save this answer.
Have a look at the jQuery API:
The JavaScript submit event does not bubble in Internet Explorer. However, scripts that rely on event delegation with the submit event will work consistently across browsers as of jQuery 1.4, which has normalized the event's behavior. (api.jquery.com/submit)
The submit
event does not bubble in Internet Explorer, so the document
element will never be notified of submit
events. jQuery will normalise this for you, but only if you use event delegation. This is not difficult to do, and in fact may make your code nicer, if there is more complexity to it:
$(document).on('submit', 'form', function(e) {
alert('clicked');
e.preventDefault();
});
This keeps the advantages of binding to the document
(e.g. capturing events on elements that don't yet exist), while making it possible in IE. Note that this
, within the event handler, is now the form
element, not the document
.
Attach the listener to your forms
$("form").submit(function(e) {
alert('clicked');
e.preventDefault();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With