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>
.....
8
  • because it's more easy to use in js form Commented Jun 14, 2019 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 Commented Jun 14, 2019 at 14:11
  • 1
    Because you can have multiple classes - you cannot have multiple names Commented Jun 14, 2019 at 14:13
  • 1
    Anyway, you can not ignore the name attributes for the radio buttons. By the way, have you read my answer below? Commented Jun 14, 2019 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. Commented Jun 14, 2019 at 14:42

3 Answers 3

9

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...)

Sign up to request clarification or add additional context in comments.

4 Comments

What will $_GET['foo'] be equal to
@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, 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.
@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... :-)
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.

1 Comment

thanks, info provided is really appreciated. I would have upvoted you if my reputation was more.
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 ?

1 Comment

thanks, very helpful, if my reputation was more i would have given you an upvote

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.