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!
This is an old question, but it's worth noting that many modern JS frameworks rely on the
name
attribute. In particular, jQuery'sserialize
function: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" thanid
.id
must be unique within a page, but the samename
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.IIRC older browsers use name in place of ID, that's why it's normally included.
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.
The
name
attribute is the notation to reference specific elements within the scope of a webpage through non-DOM Javascript:The
id
attribute for the element needs to be set with the same value for the following to work:My understanding is that when Netscape created Javascript, it used the
name
attribute. The HTML spec however decided to go withid
, but keptname
for backwards compatibility. IME, using thename
attribute was required for Internet Explorer 6 support because the javascript engine in IE wouldn't read theid
attribute - only thename
though both were defined.If you don't have any javascript attached to the text fields, yes - they would be unnecessary.
I think you'll find almost every site will have inputs with the name attribute. I don't see it going away anytime soon.
source