7

I know by reading the W3C documention for HTML4.01 and HTML5 that the "name" attribute originally existed as a property of the <a> tag to permit people to link to an anchor point within a document.

However, now that all major browser venders allow linking to any html element within a document via the "id" attribute, is there still any real use for the "name" attribute? If so, how should I be using the "name" attribute?

Andrew De Andrade
  • 3,506
  • 5
  • 30
  • 35

3 Answers3

7

One thing that comes to mind are radio buttons: you have to use name to specify which ones are part of the same group.

<form>
<input type="radio" name="sex" value="male" /> Male<br />
<input type="radio" name="sex" value="female" /> Female
</form> 
nico
  • 48,112
  • 17
  • 80
  • 111
6

The name attribute is required, I think, on input elements (and their friends)...

<input type="text" name="email" value="" />
alex
  • 438,662
  • 188
  • 837
  • 957
  • 4
    To be more specific, the name is used as the key when the form is submitted. It is also how the browser knows which options buttons go with each other so that when one is selected, the others become unselected. – tster Oct 14 '10 at 06:26
0

Good question... As mentioned in other answers one obvious use is for radio buttons so that only one radio button can be selected at a time, as you can see in jQuery radio buttons - choose only one?

Along with this, in ASP.Net MVC I have found another use of the name attribute. Refer MVC which submit button has been pressed

<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />

From http://www.w3schools.com/tags/att_button_name.asp

The name attribute specifies the name for a element.

The name attribute is used to reference form-data after the form has been submitted, or to reference the element in a JavaScript.

Tip: Several elements can share the same name. This allows you to have several buttons with equal names, which can submit different values when used in a form.

Other References

  1. HTML5 How To Skip Navigation When Name Attribute Is Obsolete
  2. HTML5 Obsolete features
  3. HTML input - name vs. id
Community
  • 1
  • 1
LCJ
  • 20,854
  • 59
  • 228
  • 387
  • Also refer http://stackoverflow.com/questions/21116999/posting-form-to-different-mvc-post-action-depending-on-the-clicked-submit-button – LCJ Jun 11 '14 at 18:36