3

If the purpose of the name attribute in HTML forms only to reference the element such as input, why couldn't we have simply used id or class attributes? Was there some not so obvious reasoning behind creating the name attribute.

.....html
 Name: <input type="text" name="fullname"><br>
.....
Deski Rey
  • 131
  • 2
  • 10
  • because it's more easy to use in js form – Mister Jojo Jun 14 '19 at 14:07
  • because you may have multiple elements with the same name and you wouldn't use class for this as that is used primarily for css and name is used to identify the data that has been submitted – Pete Jun 14 '19 at 14:11
  • @MisterJojo any examples? – Deski Rey Jun 14 '19 at 14:11
  • 1
    Because you can have multiple classes - you cannot have multiple names – Pete Jun 14 '19 at 14:13
  • @Pete regarding classes, lot of things are used with css does that mean they should not be used for data submission ? – Deski Rey Jun 14 '19 at 14:16
  • Also, only items with a `name` attribute get submitted with the `form`. But, you may still need to reference these items in JS so you set an `id` on them. Also, `labels` are linked via the `id` field. `Labels` couldn't be linked via `name` as that isn't always unique (or even always there). – MER Jun 14 '19 at 14:28
  • 1
    Anyway, you can not ignore the name attributes for the radio buttons. By the way, have you read my answer below? – Mister Jojo Jun 14 '19 at 14:31
  • 3
    Not to mention that `class` and `id` were only introduced with HTML4 - `name` pre-dates them both; so you're initial question of *"why couldn't we have simply used id or class attributes? Was there some not so obvious reasoning behind **creating the name attribute**."* is based on a faulty assumption. – CD001 Jun 14 '19 at 14:42

3 Answers3

8

name, id, and class have fundamentally different purposes.

  • name is for form element names and is not required to be unique (in fact, sometimes you need to reuse the same name).
  • id is, as the name indicates, an identifier for an element and must be unique in the document as a whole.
  • class is primarily for presentation (although it sometimes used for hooking up client-side UI as well) and contains a list of classes for the element, not just a single class. If you used class for form field identification, which of the possibly-several classes would you use?

Ways you might use the same name on more than one field:

  1. You can use the same name in different forms without there being any conflict.

  2. You use the same name within a form for radio buttons that should be grouped together.

  3. You can use the same name within a form for multiple other kinds of elements whose values should all be sent to the server.

In contrast, id must be unique — not only within the form, but within the entire document.

#3 might need an example for clarity. Suppose you have:

<form action="example" method="get">
    <input type="text" name="foo">
    <input type="text" name="foo">
    <input type="text" name="foo">
    <input type="submit" value="Send">
</form>

If the user fills in a, b, and c in those three text fields and sends the form, this is the query string:

?foo=a&foo=b&foo=c

Notice that foo was repeated. The receiving resource can access all three of those values. (Perhaps you're listing tags for a post, or all of your children's names, or...)

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • What will `$_GET['foo']` be equal to – bakero98 Jun 14 '19 at 14:50
  • @bakero98 - I don't do PHP. But IIRC, if you were targeting a PHP backend, you'd use a naming convention to tell PHP that you were repeating the field name: `name="foo[]"`. Then PHP provides some kind of array of the results, something like that. Different backend technologies handle it in different ways. – T.J. Crowder Jun 14 '19 at 14:55
  • @T.J.Crowder, regarding your question - If you used class for form field identification, which of the possibly-several classes would you use? My answer is - In Html there is already convention of having multiple classes assigned to the same element, one class could be a common class one could be unique to that element only. – Deski Rey Jun 14 '19 at 16:00
  • @DeskiRey - I don't see any reason to overload class like that. We already have `name` (and have had since before `class`), which is specific to its purpose, not mixed up in styling, and doesn't hold multiple values. So... :-) – T.J. Crowder Jun 14 '19 at 16:35
2

There are some differences between name and id

Name cannot be applied to every element, id can

Id is unique and can be applied to every existent dom element and the name can only be setted for the following elements: <a>, <applet>, <button>, <form>, <frame>, <iframe>, <img>, <input>, <map>, <meta>, <object>, <param>, <select>, and <textarea>

The id should be unique, the name not

The id should be unique because the function document.getElementById(id) will only retrieve one element. The function document.getElementsByName(name); will retreive all dom elements with a given name

In a form dom element, the name of the element is added to the data send to the browser, the id doesn't

Every input dom element that has a name, if he is inside of a form element, in the form submission will send the values of all inputs to the server. You can check this in here.

Ricardo Rocha
  • 14,612
  • 20
  • 74
  • 130
1

@MisterJojo any examples? – Deski Rey

Exemple 1:

const MyForm = document.getElementById('my-form')

MyForm.onsubmit = e=>{
  e.preventDefault()

  console.log( MyForm.username.value )  // direct access to value with name
}
<form id="my-form">
  <input type="text" name="username">

  <button type="submit">submit</button>
</form>

Exemple 2:

const MyForm = document.getElementById('my-form')

MyForm.onsubmit = e=>{
  e.preventDefault()

  console.clear()
  console.log( MyForm.RadioABC.value )  // direct access to value with name
}
<form id="my-form">

A: <input type="radio" name="RadioABC" value="A" checked >
B: <input type="radio" name="RadioABC" value="B">
C: <input type="radio" name="RadioABC" value="C">

  <button type="submit">submit</button>
</form>

want more ?

Mister Jojo
  • 20,093
  • 6
  • 21
  • 40