These forums are currently read-only due to receiving more spam than actual discussion. Sorry.

It is currently Sat Dec 02, 2017 4:05 pm Advanced search

How to determine which button submitted a form?

Do you think the HTML spec should do something differently? You can discuss spec feedback here, but you should send it to the WHATWG mailing list or file a bug in the W3C bugzilla for it to be considered.

How to determine which button submitted a form?

Postby David Grudl » Thu Aug 12, 2010 12:51 am

Is there a way in HTML5 to determine which button submitted a form from within an onsubmit handler?
User avatar
David Grudl
<h6>
 
Posts: 3
Joined: Thu Aug 12, 2010 12:46 am

Postby JAB Creations » Thu Aug 12, 2010 12:57 am

Run this from your anonymous onload function; works in IE 5.5+ and all other browsers that don't irk web designers...

Code: Select all
function events_forms(id)
{
if (document.getElementById(id))
{
  //alert(id);
  var forms = document.getElementById(id).getElementsByTagName('form');
  var submit = document.getElementById(id).getElementsByTagName('input');

  for (var i=0;i<forms.length;i++)
  {
   var f = forms[i].id;

   if (window.addEventListener)
   {
    forms[i].addEventListener('submit',function(e,f) {e.preventDefault(); ajax_post_parameters(e.target.id); events_forms_array(this.id);},false);
   }
   else if (window.attachEvent)
   {
    forms[i].attachEvent('onsubmit',function(e,f) {e.returnValue=false; ajax_post_parameters(e.srcElement.id); events_forms_array(e.srcElement.id);});
   }
  }

  for (var i=0;i<submit.length;i++)
  {
   if (submit[i].type=='submit')
   {
    if (window.addEventListener)
    {
     submit[i].addEventListener('click', function(e) {option.name = this.name;},false);
    }
    else if (window.attachEvent)
    {
     submit[i].attachEvent('onclick', function(e) {option.name = e.srcElement.name;});
    }
   }
  }
}
else {alert('Error: no element with the id \''+id+'\' was found.');}
}

alert(option.name);
User avatar
JAB Creations
<aside>
 
Posts: 566
Joined: Tue Mar 13, 2007 4:48 am
Location: Sarasota Florida, USA

Postby David Grudl » Thu Aug 12, 2010 1:03 am

Thanks for your reply. I use a workaround too, but it would be nice if this could HTML5 support better. For example by attribute "form.submitter".
User avatar
David Grudl
<h6>
 
Posts: 3
Joined: Thu Aug 12, 2010 12:46 am

Postby JAB Creations » Thu Aug 12, 2010 2:15 am

You can't verb with a noun. HTML is the noun construct, CSS modifies nouns as an adjective, and verbs convey actions. When you want to determine the last id or name you are taking an action to achieve a goal.
User avatar
JAB Creations
<aside>
 
Posts: 566
Joined: Tue Mar 13, 2007 4:48 am
Location: Sarasota Florida, USA

Postby David Grudl » Thu Aug 12, 2010 2:44 pm

HTML5 is the noun and verb, it specifies a lot of properties of DOM. And form.submitter could be very useful property.
User avatar
David Grudl
<h6>
 
Posts: 3
Joined: Thu Aug 12, 2010 12:46 am

Postby JAB Creations » Thu Aug 12, 2010 7:15 pm

I wouldn't say that what I'm saying is the end of the line fact however when you change (X)HTML or CSS it generally adds more properties to the DOM. Also as various specifications mature the vendor-specific methods and properties tend to be removed in favor of the then standardized counter-parts.

If you want to point out an example in the spec that intentionally goes out of the way to start changing the DOM directly I'd be happy to take a look at it.

zcorpan would be able to follow up on this with a much more decisive answer then I could off hand however.
User avatar
JAB Creations
<aside>
 
Posts: 566
Joined: Tue Mar 13, 2007 4:48 am
Location: Sarasota Florida, USA


Return to Feedback on the Specs

Who is online

Users browsing this forum: No registered users and 1 guest