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?
Have a look at the jQuery API:
The
submitevent does not bubble in Internet Explorer, so thedocumentelement will never be notified ofsubmitevents. 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: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 thatthis, within the event handler, is now theformelement, not thedocument.