<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://www.w3.org/Bugs/Public/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4"
          urlbase="https://www.w3.org/Bugs/Public/"
          
          maintainer="sysbot+bugzilla@w3.org"
>

    <bug>
          <bug_id>23320</bug_id>
          
          <creation_ts>2013-09-22 06:18:22 +0000</creation_ts>
          <short_desc>forms: Have the form &apos;submit&apos; event include a reference to the submit button used by the user to submit the form</short_desc>
          <delta_ts>2014-09-30 16:37:07 +0000</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WHATWG</product>
          <component>HTML</component>
          <version>unspecified</version>
          <rep_platform>All</rep_platform>
          <op_sys>All</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>WONTFIX</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P3</priority>
          <bug_severity>enhancement</bug_severity>
          <target_milestone>Unsorted</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Giorgio">giorgio.liscio</reporter>
          <assigned_to name="Ian &apos;Hixie&apos; Hickson">ian</assigned_to>
          <cc>ian</cc>
    
    <cc>memmie</cc>
    
    <cc>mike</cc>
          
          <qa_contact>contributor</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>93621</commentid>
    <comment_count>0</comment_count>
    <who name="Giorgio">giorgio.liscio</who>
    <bug_when>2013-09-22 06:18:22 +0000</bug_when>
    <thetext>hello... how about referencing the button that generated the &quot;submit&quot; event in submitEvent.relatedTarget ?

example:

&lt;button type=&quot;submit&quot; name=&quot;action&quot; value=&quot;listAll&quot;&gt;List All Selected&lt;/button&gt;
&lt;button type=&quot;submit&quot; name=&quot;action&quot; value=&quot;deleteAll&quot;&gt;Delete All Selected&lt;/button&gt;

myform.addEventListener(&quot;submit&quot;, function(event){

       var fd = new FormData();

       fd.append(&quot;some&quot;,&quot;data&quot;);

       if(event.relatedTarget)
              fd.append(event.relatedTarget.name, event.relatedTarget.value);

       // ... results in fd.append(&quot;action&quot;, &quot;listAll&quot;); or deleteAll, accordingly to which button was pressed

       // ...

       event.preventDefault();

}, false);</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>93673</commentid>
    <comment_count>1</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2013-09-23 19:42:20 +0000</bug_when>
    <thetext>Not a bad idea, but the &apos;submit&apos; event uses Event currently, which doesn&apos;t have a relatedTarget, so this would have to involve a new Event interface type.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>93704</commentid>
    <comment_count>2</comment_count>
    <who name="Giorgio">giorgio.liscio</who>
    <bug_when>2013-09-24 03:34:02 +0000</bug_when>
    <thetext>another solution could be referencing the submit button in some property of the HTMLFormElement interface, in the same way document.activeElement works

something like myform.submittedBy

imho not very cool though</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>93755</commentid>
    <comment_count>3</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2013-09-24 22:21:34 +0000</bug_when>
    <thetext>Yeah doing it on the event seems better.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>93777</commentid>
    <comment_count>4</comment_count>
    <who name="Giorgio">giorgio.liscio</who>
    <bug_when>2013-09-25 05:21:36 +0000</bug_when>
    <thetext>another solution could be include relatedTarget in the Event interface

anyway...
I should have written a best use-case for this feature request, so here it is

&lt;button name=&quot;action&quot; value=&quot;listAll&quot;&gt;List All Selected&lt;/button&gt;
&lt;button name=&quot;action&quot; value=&quot;deleteAll&quot;&gt;Delete All Selected&lt;/button&gt;

myform.addEventListener(&quot;submit&quot;, function(event){

       // [I&apos;m guessing] FormData doesn&apos;t include buttons name/value pairs:

       var fd = new FormData(this);

       if(event.relatedTarget)
              fd.append(event.relatedTarget.name, event.relatedTarget.value);
       else
              fd.append(&quot;action&quot;, &quot;listAll&quot;);

       event.preventDefault();

       var xhr = ...;
       // ...
       xhr.send(fd);

}, false);</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>94271</commentid>
    <comment_count>5</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2013-10-03 20:31:56 +0000</bug_when>
    <thetext>Are there any browser vendors interested in implementing something like this?


(In the meantime you can work around the lack of this feature by doing this:

 &lt;form onclick=&quot;if (event.target.type == &apos;submit&apos;) this._button = event.target&quot;&gt;

...on the &lt;form&gt; element. This sets form._button to the submit button in time for the onsubmit handler.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>111907</commentid>
    <comment_count>6</comment_count>
    <who name="Memmie Lenglet">memmie</who>
    <bug_when>2014-09-22 09:15:32 +0000</bug_when>
    <thetext>Actually we can&apos;t rely on anything for that. Listen clicks on `input[type=submit]` and `button[type=submit]`, use `document.activeElement`, etc.
Mozilla provide a specific property on the submission event (and few others): `event.explicitOriginalTarget` to provide a &quot;related&quot; target.
In http://www.w3.org/html/wg/drafts/html/master/forms.html#form-submission-algorithm there is a mention of a &quot;submitter&quot;.

Here there is a talk about it: http://discourse.specifiction.org/t/extend-submit-events-add-the-button-element-that-triggered-the-submit/406</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>111931</commentid>
    <comment_count>7</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2014-09-22 16:52:06 +0000</bug_when>
    <thetext>I don&apos;t understand. Why doesn&apos;t the proposed markup work? I tried it and it seemed to work fine on Chrome and Firefox at least. It even works with implicit submission (where you hit enter in a text field and it picks the default submit button).

http://software.hixie.ch/utilities/js/live-dom-viewer/saved/3192</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>111934</commentid>
    <comment_count>8</comment_count>
    <who name="Memmie Lenglet">memmie</who>
    <bug_when>2014-09-22 17:12:32 +0000</bug_when>
    <thetext>This don&apos;t work:

- the submitter is not only `[type=submit]` but also: `input[type=submit]`, `input[type=image]`, `button[type=submit]`
- the event target can be a descendant of a button: `&lt;button&gt;&lt;span&gt;Submit&lt;/span&gt;&lt;/button&gt;`
- this works for [&quot;authentic click activation&quot;][1] (direct click or key stroke) and [&quot;synthetic click activation&quot;][2] (click via `element.click()` or `element.dispatchEvent(new MouseEvent(...))`) but not [&quot;Implicit submission&quot;][3] (hitting enter or space key, depends implementation, when an other form field focused)

This will work on some cases, but not all.


[1]: http://www.w3.org/html/wg/drafts/html/master/editing.html#run-authentic-click-activation-steps
[2]: http://www.w3.org/html/wg/drafts/html/master/editing.html#run-synthetic-click-activation-steps
[3]: http://www.w3.org/html/wg/drafts/html/master/forms.html#implicit-submission</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>112037</commentid>
    <comment_count>9</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2014-09-23 18:17:03 +0000</bug_when>
    <thetext>The example can be easily updated to work with type=image or nested content in &lt;button&gt;s if you need that. It already works for implicit submission.

Here&apos;s an example that does everything you mentioned:
   http://software.hixie.ch/utilities/js/live-dom-viewer/?saved=3199


Note that dispatchEvent(new MouseEvent()) is not a valid way of submitting a form. It should have no effect; it has some effect in some browsers due to a bug, which we are trying to fix.

BTW the links you are giving to the spec are to the forked out-of-date version of the spec. The spec I edit is at: http://html.spec.whatwg.org/multipage/</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>112050</commentid>
    <comment_count>10</comment_count>
    <who name="Memmie Lenglet">memmie</who>
    <bug_when>2014-09-23 23:18:02 +0000</bug_when>
    <thetext>OK! I miss one important thing. In [synthetic click activation steps][1] there is:
&quot;4. Fire a click event at the element&quot;.
That means, for browsers that implement HTML5, we can listen implicit submission with click events.

I tried your example and that work correctly on:

- Safari 7.1
- Firefox 33.0
- Chrome 38.0
- Safari Mobile 7.2
- Chrome iOS 37

But not on IE 9 (I got an error &quot;error: Unable to get value of the property &apos;value&apos;: object is null or undefined on line 1&quot;, but I don&apos;t have the time to found why click don&apos;t work).

Thanks

[1]: https://html.spec.whatwg.org/multipage/interaction.html#run-synthetic-click-activation-steps</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>112085</commentid>
    <comment_count>11</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2014-09-24 17:09:32 +0000</bug_when>
    <thetext>Well I presume that&apos;s because IE9 doesn&apos;t implement the specs properly. Does it work in more recent IEs?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>112143</commentid>
    <comment_count>12</comment_count>
    <who name="Memmie Lenglet">memmie</who>
    <bug_when>2014-09-24 22:22:18 +0000</bug_when>
    <thetext>Yes, I confirm that works in IE10</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>112184</commentid>
    <comment_count>13</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2014-09-25 17:37:38 +0000</bug_when>
    <thetext>Cool, ok, thanks.

Given how simple it is to achieve this, and how widely supported it is (especially compared to a new API, which nobody implements), maybe this feature request is something we shouldn&apos;t add to the platform.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>112479</commentid>
    <comment_count>14</comment_count>
    <who name="Ian &apos;Hixie&apos; Hickson">ian</who>
    <bug_when>2014-09-30 16:37:07 +0000</bug_when>
    <thetext>Closing per comment 13. If there&apos;s a compelling reason why the simple script above isn&apos;t a satisfactory solution, please reopen the bug. Thanks!</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>