Bug Tracker

Ticket #4930 (closed bug: wontfix)

Opened 5 years ago

Last modified 3 years ago

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:
Blocking: Blocked by:

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

onsubmit-jquery-issue.html Download (749 bytes) - added by nbelaevski 5 years ago.

Change History

Changed 5 years ago by nbelaevski

comment:1 follow-up: ↓ 2 Changed 5 years ago by dmethvin

  • Status changed from new to closed
  • Resolution set to invalid

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.

comment:2 in reply to: ↑ 1 Changed 5 years ago by warp

  • Status changed from closed to reopened
  • Resolution invalid deleted

Replying to 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.

comment:3 Changed 5 years ago by warp

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;
		}

comment:4 Changed 5 years ago by john

  • Status changed from reopened to closed
  • Resolution set to wontfix

.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!

comment:5 follow-up: ↓ 6 Changed 3 years ago by 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.

comment:6 in reply to: ↑ 5 Changed 3 years ago by cliffred

Replying to 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.

Note: See TracTickets for help on using tickets.