Skip to main content

Bug Tracker

Side navigation

#4930 closed bug (wontfix)

Opened July 20, 2009 06:57PM UTC

Closed December 05, 2009 04:20AM UTC

Last modified January 10, 2012 04:14PM UTC

Inline "onsubmit" handler is not called when "submit" event is triggered

Reported by: nbelaevski Owned by:
Priority: major Milestone: 1.4
Component: unfiled Version: 1.3.2
Keywords: Cc:
Blocked by: Blocking:
Description

Triggering "submit" event on FORM element submits form, but doesn't check bound "onsubmit" handler. Only callbacks attached by jQuery.bind() are called.

Attached test case triggers "click" and "submit" events for comparison.

Attachments (1)
Change History (6)

Changed July 21, 2009 02:04AM UTC by dmethvin comment:1

resolution: → invalid
status: newclosed

A form has both a submit() method and an onsubmit event. Unlike many/most other elements, browsers do not fire the element's onsubmit event when the submit() method is called. When you use

jQuery().trigger("submit")
jQuery is calling the native submit() method for you but since it doesn't "see" the native event and the native submit() doesn't trigger the native onsubmit, it is not called. However, this is a browser quirk and outside the scope of jQuery. If you want jQuery to execute an event on submit, define the event with jQuery.

Changed September 02, 2009 09:01AM UTC by warp comment:2

resolution: invalid
status: closedreopened

Replying to [comment:1 dmethvin]:

However, this is a browser quirk and outside the scope of jQuery. If you want jQuery to execute an event on submit, define the event with jQuery.

I do not agree - the documentation implies that this should work and there IS code in jQuery to handle triggering onevent-functions - it just does not cover onsubmit yet:

In jQuery 1.3.2 it's the following line:

if ( (!elem[type] || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )

event.result = false;

In subversion jQuery we have in events.js line 256 following:

// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)

if ( (!nativeFn || (jQuery.nodeName(elem, 'a') && type === "click")) && nativeHandler && nativeHandler.apply( elem, data ) === false ) {

Obviously, the only reason onsubmit is not called is because of the if( !nativeFn) check. There's already special handling for a.click, I cannot see they reason why no || type === "submit" case could be added here for improved API consistency.

Changed September 02, 2009 09:03AM UTC by warp comment:3

Sorry for the broken code excerpts before:

jquery 1.3.2:

if ( (!elem[type] || (jQuery.nodeName(elem, 'a') && type == "click")) && elem["on"+type] && elem["on"+type].apply( elem, data ) === false )
			event.result = false;

jQuery Subversion:

// Handle triggering native .onfoo handlers (and on links since we don't call .click() for links)
		if ( (!nativeFn || (jQuery.nodeName(elem, 'a') && type === "click")) && nativeHandler && nativeHandler.apply( elem, data ) === false ) {
			event.result = false;
		}

Changed December 05, 2009 04:20AM UTC by john comment:4

resolution: → wontfix
status: reopenedclosed

.submit() doesn't appear to trigger the native onsubmit. Unfortunately we can't not execute the .submit() (like we can with the a click) so I guess we'll just have to bite the bullet on this one. Sorry!

Changed December 19, 2011 10:56AM UTC by arrcher@hotmail.fr comment:5

This seems to have changed, I switched from jquery 1.4.2 to 1.7.1, and now the onsubmit is called by jquery.

I would like to know since what version and why.

Changed January 10, 2012 04:14PM UTC by cliffred comment:6

Replying to [comment:5 arrcher@…]:

This seems to have changed, I switched from jquery 1.4.2 to 1.7.1, and now the onsubmit is called by jquery. I would like to know since what version and why.

I also noticed this behavior has changed when I switched from 1.3.2 to 1.7.1. So I made a little test to find out since what version this has been changed. Strangely enough I came to the conclusion this has been changed in version 1.4.1. I used the following html to find out:

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#myform").submit();
});
</script>
</head>
<body>
<form id="myform" onsubmit="alert('onsubmit')" action="">
  <input type="submit" />
</form>
</body>
</html>

1.4 is the latest version in which the alert box isn't shown. In 1.4.1 and later it is.