5

I've noticed a lot of websites with form(s) containing input fields whose "name" attribute is specified even if it isn't used for styling or scripting purpose!

Moreover, according to the official document about the form in HTML document ...

name = cdata [CI] This attribute names the element so that it may be referred to from style sheets or scripts. Note. This attribute has been included for backwards compatibility. Applications should use the id attribute to identify elements.

So, my question is: should this attribute used only for styling and scripting purposes?

Thanks in advance!


EDIT: In particular, could be avoided the use of this attribute with input fields of "text" type (when there aren't no styling or scripting purposes)?


EDIT 2: So, you have almost confirmed what I had thought about: the "name" attribute will be deprecated in further HTML specifications/standards!!!??? It is still "alive" only for backwards compatibility ... in some cases can be avoided but there are still some cases (such as the radio button) where it is necessary!

3
  • 3
    Your citation is for the FORM element, not input elements. You'll still need to give input elements a name in order for your server-side code to correctly read the submitted information. Commented Nov 28, 2009 at 18:47
  • I'm sorry you are right! About the submit process I agree, but my question is more general, in fact as underlined from the below answers, there are cases where this attribute is unnecessary! However thanks! Commented Nov 28, 2009 at 19:12
  • It is incorrect to say name is only there for backwards compatibility - there are some good answers and discussion of why below. It has a definite purpose, so I think it's unlikely it will ever be deprecated. I suggest you just edit the question to be a question, or correct what you've said in "edit 2". Commented Oct 7, 2021 at 6:00

6 Answers 6

5

I think you'll find almost every site will have inputs with the name attribute. I don't see it going away anytime soon.

The name attribute specifies a name for an input element.

The name attribute is used to identify form data after it has been submitted to the server, or to reference form data using JavaScript on the client side.

Note: Only form elements with a name attribute will have their values passed when submitting a form.

source

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

2 Comments

You have underlined a good point! However, my question comes out after I've seen some input fields that contain this attribute (and some others) where is not necessary ... so I think that this is a bad developing practice, a matter of redundancy!
It depends, if the input is in a form and you don't want/need to submit the data, don't use the name attribute. If the input is outside of a form, you don't need it at all. If it's going to be deprecated? I really doubt it, but it's already an optional attribute so you only need to use it if you want to (or if you want to pass data to the server).
3

The name attribute is the notation to reference specific elements within the scope of a webpage through non-DOM Javascript:

document.forms['your_form'].elements['aa']

The id attribute for the element needs to be set with the same value for the following to work:

document.getElementById('aa')

My understanding is that when Netscape created Javascript, it used the name attribute. The HTML spec however decided to go with id, but kept name for backwards compatibility. IME, using the name attribute was required for Internet Explorer 6 support because the javascript engine in IE wouldn't read the id attribute - only the name though both were defined.

...could be avoided the use of this attribute with input fields of "text" type (when there aren't no styling or scripting purposes)?

If you don't have any javascript attached to the text fields, yes - they would be unnecessary.

2 Comments

Thanks! So you have confirmed what I had thought about: the "name" attribute in future will be probably deprecated!!!???
@BitDrink: Once we're rid of Internet Explorer 6, the deprecation should be reality.
2

There are differences between id and name attributes. An id is applicable to any element in the HTML document while a name is relevant for input fields only. An id is required by standard to be unique in a page (though not necessarily followed in all web pages). Different elements may have same name though. One particular case comes into mind is the radio button. All radio buttons should have the same name and the value of the one selected would be given back to the form. So you can see that name still has significance in HTML form processing.

I have seen in automatic HTML form generation systems (like zope.formlib), that id and name attributes both are automatically generated for different types of input widgets. Such automatic form generation systems take proper care of all the nuances associated with differences in id and name attributes. They also do things like generating a hidden input element for each checkbox element. So wherever possible, I try to use some sort of automatic HTML form generation mechanism and let it take care of the issues involved.

2 Comments

Thanks, the radio button is a good example! However, I don't agree with automatic generation of markup code!
@BitDrink Well, I don't agree with generating HTML when it's something small like a form, but many websites use server-side scripting to output large amounts of HTML that couldn't possibly be made by hand, such as search engines. There are also things like the Google Web Toolkit, which goes to the point of automatically generating javascript from java.
1

This is an old question, but it's worth noting that many modern JS frameworks rely on the name attribute. In particular, jQuery's serialize function:

For a form element's value to be included in the serialized string, the element must have a name attribute.

If anything, name seems to be making a comeback.

It's also worth noting that the name attribute is useful because it has slightly more "scope" than id. id must be unique within a page, but the same name can be used multiple times. This makes it useful when you have multiple forms on the same page, or when you want to reference a group of checkboxes, etc.

1 Comment

It's true that name is mistakenly used for scripting and styling when id is better. But for gathering and submitting data from a form, name is the right thing. It never went away!
1

Your reference to HTML 4 is way out of date. Here's WHATWG's discussion of name - https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#naming-form-controls:-the-name-attribute

name should not be used for scripting or styling. That is what id is for as you have said.

But name is not obsolete and I see no prospect that it might be removed from HTML. Its purpose is specifically to submit data to a server.

id should be unique for your HTML document to be well formed. name might not be. You might have a set of three radio buttons, let's say "Small", "Medium" and "Large". You could give them three unique values for their id attributes, so if your user interacts with other elements you could change the selected radio button in script. But when the form is submitted, you want only one value to be submitted, so you give all three radio buttons the name value for name.

<input type="radio" id="size-small" name="size">Small</input>
<input type="radio" id="size-medium" name="size">Medium</input>
<input type="radio" id="size-large" name="size">Large</input>

Comments

0

IIRC older browsers use name in place of ID, that's why it's normally included.

1 Comment

id should be sued for scripting and styling, but name does have a purpose - to submit data to a server

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.