Andrew De Andrade
Andrew De Andrade

Reputation: 3616

When should I use the name attribute in HTML4/HTML5?

I know by reading the W3C documentation 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 vendors 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?

Upvotes: 7

Views: 1245

Answers (3)

LCJ
LCJ

Reputation: 22661

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

Upvotes: 0

nico
nico

Reputation: 51690

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> 

Upvotes: 7

alex
alex

Reputation: 490647

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

<input type="text" name="email" value="" />

Upvotes: 6

Related Questions