Do submit buttons require a name or id? Posted by Mischa S.

Back

Date: February 13, 2012

Nope.  It does not.  If you want to reference it directly via jQuery, you're going to use the ID, but the name is irrelevant.  How did this simple piece of information (which seems like a pointless note....at first) come across my keyboard you ask?  Or, perhaps you didn't.  No bother, I'm going to tell you anyway.

I was using a button to validate some input fields and was using the .click() jQuery method to do so.  I finished the form.  Finished my simple validation criteria.  I was ready to write my simple jQuery .click() and validate method.

$("form#myform input[type=submit]").click(function(){

 //do my validation here

 //if my validation is successful, submit my form

$("#myform").submit();

});

What happens?  A whole lot of nothing.  So, I double check that it's running. By sticking an alert in there:

alert('what did I fat finger?');

To my vague surprise, it fires....I move it after the validation.  It still fires.  I move it after the submit, it still fires.  Finally I move it into the submit and return true so it will still submit like this:

$("#myform").submit(function(){

  //alert me

  alert('OK. Seriously now, wtf!?!');

return true;

});

And, yup you guessed it.  It still fired.  So, what you have already guessed by now (probably, since I put the answer in the title of this post) was that, because I had set the name and id of the submit button to 'submit'. 

When I called:

$("#myform").submit(); 

it was actually calling the submit button, but that doesn't make sense, so jQuery was just failing gracefully.

<input id="submit" name="submit" type="submit" value="submit" />

That was an hour of my life, that I won't have back, because I forgot one of the golden rules. 

"Woe unto the fool that uses reserved words"

Did I already know this.  Yes.  Did I forget?  Yup.  But, I don't believe I had ever actually read it anywhere.  If you must name your submit button, name it something besides 'submit'.