11

Should I use the name attribute only for form controls? Is it deprecated today in HTML5?

I noticed similar questions are outdated, so I'm a little confused.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
cookya
  • 3,019
  • 7
  • 26
  • 37
  • https://html.spec.whatwg.org/multipage/indices.html#attributes-3 has the authoritative list of which attributes are allowed in HTML and which elements allow them. – sideshowbarker Nov 22 '16 at 10:11

3 Answers3

12

Should I use the name attribute only for form controls?

No. It is only on <a> elements that it is obsolete (and replaced with id on any element).

The attributes index in the HTML5 spec shows where it should still be used.

Some uses of it which were never standard should be replaced with class.

Is it deprecated today in HTML5?

HTML5 doesn't use the term "deprecated". It describes it as obsolete.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • 1
    In 2018, the attributes table is no longer present in that document. Aside from an [archived copy](https://web.archive.org/web/20161116114534/https://www.w3.org/TR/html5/index.html#attributes-1) you can find such a table now at https://html.spec.whatwg.org/multipage/indices.html#attributes-3 – Jacob C. Feb 23 '18 at 20:47
5

I checked Mozilla Developer Network (MDN) and found this:

name

<button>, <form>, <fieldset>, <iframe>, <input>, <keygen>, <object>, <output>, <select>, <textarea>, <map>, <meta>, <param>

Name of the element. For example used by the server to identify the fields in form submits.

So you should use this attribute only on these elements. Any other element with a name attribute is not correct. For more info about what attributes exists and where/who/why to use it check the full documentation:

HTML attribute reference

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sandrina-p
  • 3,794
  • 8
  • 32
  • 60
4

It seems that using <a name=""> is depreciated - which means you shouldn't use it for your anchor tags.

On forms you should still use name="" to label your form controls - AngularJS requires you to use name="" if you ever want to use validation.

Some more information here and here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tom Johnson
  • 679
  • 5
  • 15