68

I remember reading in the spec once that both the id attribute and the name attribute share the same namespace and have to be unique. Henceforth I've always tried to fulfill this requirement in my applications, dreading even to give the same id and name to the same element.

But lately I've started working with ASP.NET MVC 3, and it (like PHP) can use the same name attribute on several input controls to form a collection of values at server-side. I tried to look up the relevant section in the spec - but failed to find it. Perhaps I have misunderstood something then, or read the wrong documentation?

How is it then? I want to produce as valid HTML as possible (both 4.01 and 5 in different apps). Can I use this trick without fear? Or would I be violating something and should better stick to unique values?

Vilx-
  • 104,512
  • 87
  • 279
  • 422

4 Answers4

66

The name attribute is only valid on the <form> and form elements (<input>,<textarea> and <select>). It's used to specify the name to associate with the name/value pair that is submitted on a form post.

For example:

<input type="checkbox" name="foo" value="1" />

if checked will submit foo=1. In the DOM you can reference form elements from the form.elements collection by specifying the name as the index. If name is not unique, the collection returns an array of elements rather than the element. Modern DOM's support looking up form elements by name as:

 document.getElementsByName(nameValue)

note: it always returns an array even if only one element is found.

id attribute is from the XML world, and is a unique id for any node, not just form elements. Unlike the name attribute it is valid on any HTML node. Also like the name attribute, it must follow the valid identifier rules. The identifier should start with an alpha, and only contain alpha ([a-zA-Z]), numbers, hyphen, underscore and colons (note ASP.NET breaks this rule by starting reserved IDs with a underscore - thus they will always fail an HTML/XML lint - actually some proxies strip them). To find any HTML element by id you use:

document.getElementById(idvalue)

this only returns one DOM node.

henser
  • 3,307
  • 2
  • 36
  • 47
Kolten
  • 3,495
  • 5
  • 42
  • 59
  • 1
    Ahh, [I found it](http://www.w3.org/TR/1999/REC-html401-19991224/struct/links.html#h-12.2.3)! Seems that I had missed the context. :) Indeed, there is a handful of other elements that the `name` attribute applies to, but there it just duplicates the `id` attribute and in fact **has to be equal to it** if both are defined. Fortunately this does not apply to form elements. :) – Vilx- Apr 01 '11 at 20:49
  • 1
    id and name in html5 may contain any character except space, but must have at least one character: http://www.w3.org/TR/html5/dom.html#the-id-attribute – westor Oct 21 '15 at 15:53
25

The name attribute is not unique. For instance, it is used to group radio buttons. It represents the value of a particular form property. ids must be unique.

Ishmael
  • 30,970
  • 4
  • 37
  • 49
9

ID should be unique but you can use multiple form elements with the same NAME. This is standard for how radio buttons work so you can force one seletion of a radio button group.

Alan Barber
  • 983
  • 5
  • 15
1

Must names be unique between forms for radio input groups?

I understood that name didn't have to be unique because radio elements can share the same name, but nobody said whether or not groups of radio elements in different forms would interfere with each other or not. So I created this simple example below to test. In my browser, I can pick 2 of the 6 radios in the example below, and there are two forms. So it appears that putting them in separate forms will isolate them.

<form>
  <input type="radio" name="test" value="1">
  <input type="radio" name="test" value="2">
  <input type="radio" name="test" value="3">
</form>

<form>
  <input type="radio" name="test" value="a">
  <input type="radio" name="test" value="b">
  <input type="radio" name="test" value="c">
</form>

I also wondered if the same behavior would hold true with the newer <fieldset> element, however it doesn't seem to. I guess that makes sense because if I sent the form it would need to format the data to accommodate the name conflict somehow. I can only pick 1 among the 6 radios here:

<form>
  <fieldset name="test1">
    <legend>test1</legend>
    <input type="radio" name="test" value="1">
    <input type="radio" name="test" value="2">
    <input type="radio" name="test" value="3">
  </fieldset>
  <fieldset name="test2">
    <legend>test2</legend>
    <input type="radio" name="test" value="a">
    <input type="radio" name="test" value="b">
    <input type="radio" name="test" value="c">
  </fieldset>
</form>

I'm not sure why anyone would want to do this, but you can also associate each element with a different form by using the form=<form id> attribute. It seems to separate the radio groups again. As shown here:

<form id="a">
</form>

<form id="b">
</form>

<fieldset name="test1">
  <legend>test1</legend>
  <input form="a" type="radio" name="test" value="1">
  <input form="a" type="radio" name="test" value="2">
  <input form="a" type="radio" name="test" value="3">
</fieldset>
<fieldset name="test2">
  <legend>test2</legend>
  <input form="b" type="radio" name="test" value="a">
  <input form="b" type="radio" name="test" value="b">
  <input form="b" type="radio" name="test" value="c">
</fieldset>

I think ideally a fieldset would create some sort of grouping that was more than just visual, but it doesn't oh well. At least radio groups can be separated by forms.

Addendum: What does the form data look like when you use the same name within two or more fieldsets inside of one form? Let's see.

My suspicions are confirmed. There's just one 'test' parameter and fieldsets have no effect on the data at all. Try picking a radio and hitting submit.

function examine(e){
  e.preventDefault()
  e.stopPropagation()
  var formData = new FormData(e.target)
  ,formProps = Object.fromEntries(formData)
  document.getElementById('out').innerText = JSON.stringify(formProps,null,2)
  return false;
}
document.getElementById('mainform').addEventListener('submit',examine)
<form id="mainform">
  <fieldset name="test1">
    <legend>test1</legend>
    <input type="radio" name="test" value="1">
    <input type="radio" name="test" value="2">
    <input type="radio" name="test" value="3">
  </fieldset>
  <fieldset name="test2">
    <legend>test2</legend>
    <input type="radio" name="test" value="a">
    <input type="radio" name="test" value="b">
    <input type="radio" name="test" value="c">
  </fieldset>
  <button type="submit">examine</button>
</form>
<pre id="out"></pre>
ADJenks
  • 2,973
  • 27
  • 38
  • A fieldset might create a visual grouping, but what would the request look like when the form was submitted? What value would the `test` field take? Or would there be two `test` fields sent to the server? I think this is the main reason for the behaviour you're observing. Anyways... this answer doesn't really answer the question, unfortunately... – Vilx- Sep 16 '22 at 22:33
  • @Vilx- I know it didn't answer the original question, but I wanted to add to the details. Nobody seemed to clarify whether or not you could use the same radio name in two different forms or if they would interfere with each other, so I wanted to create a demo to clarify this for myself and others. I thought it was a useful addition to the other information on this page. Your question is also good. I've added another demo at the end to answer your question and show that there would only be one field created. – ADJenks Sep 19 '22 at 02:13
  • I also tested by trying to actually submit the form and there seemed to still be only one parameter for two fieldsets in one form. This rules out the possibility that creating a FormData object might destroy one. – ADJenks Sep 19 '22 at 02:15