Skip to main content

Bug Tracker

Side navigation

#6337 closed bug (fixed)

Opened March 23, 2010 03:24PM UTC

Closed September 24, 2010 10:03PM UTC

Button click within form causes submit event even though return false is there

Reported by: margieroginski Owned by: dmethvin
Priority: undecided Milestone: 1.4.3
Component: event Version: 1.4.2
Keywords: submit event return false Cc:
Blocked by: Blocking:
Description

I have a small test case below where I create a button inside a form and set its onclick hander to my js function, show_input(), followed by a "return false", like this:

Immediately following that is a one line script that clicks the button.


	    <button id="testbutton1" onclick="show_input(); return false;">
	       Test
	    </button>
	    <script type="text/javascript">
              $('#testbutton1').click();
	    </script>

In jquery 1.3.2, when the .click() occurs, it calls show_input() and no submit event occurs aftwards, due to the return false. But in in Jquery 1.4.2, it calls show_input() and then the submit event fires (it does a GET), as if the return false is not there.

If I manually click the button, I do not see the submit event, so this seems to be somehow related to the click being inline.

Here is the exact html that can be used to replicate it.

<html>
  <head>
    <script src="/site_media/js/jquery-1.3.2.js" type="text/javascript"></script>
    <script type="text/javascript">
      function show_input() {
        console.log("hello, world");
      }
    </script>
  </head>

  <body>
  <div id="container">
      <form>
      <div class="taskform_row_description form-row">
	    <button id="testbutton1" onclick="show_input(); return false;">
	       Test
	    </button>
	    <script type="text/javascript">
              $('#testbutton1').click();
	    </script>
      </div>
      </form>
  </div>
  </body>
</html>

This seems to me to be a bug since it is very incompatible with previous behavior...

Thanks,

Margie

Attachments (0)
Change History (4)

Changed June 15, 2010 01:01AM UTC by dmethvin comment:1

component: unfiledevent

Changed July 28, 2010 02:39AM UTC by dmethvin comment:2

owner: → dmethvin

It looks like the problem is in jQuery.event.trigger; the code to run inline scripts sets event.result to false but does not stopPropagation or preventDefault. I've taken the ticket and will put together a patch.

Changed August 26, 2010 01:45AM UTC by dmethvin comment:3

Changed September 24, 2010 10:03PM UTC by john comment:4

priority: → undecided
resolution: → fixed
status: newclosed

Fixed http://github.com/jquery/jquery/commit/adff8e45e255409e8f78a3347dea01bdbee0a53d

We only need to call preventDefault in this case.