Tuesday, November 15, 2011

Why do I have to type both the id AND name attributes in forms?

Q: Why do I have to type both the id AND name attributes in forms?
A: Technically (although the 3rd edition of the textbook says you do), you don't. You can use only id.
  • The name attribute specifies the name of a form.
  • The name attribute of the form element provides a way to reference the form in a script.
Tip: In XHTML, the name attribute of the tag is deprecated, and will be removed. Use the id attribute instead. (source: W3C schools)


The id attribute is a core attribute. It is not valid in base, head, html, meta, param, script, style, and title elements.
  • It is standard, and is supported by all HTML and XHTML tags.
  • The id attribute specifies a unique id for an HTML element (the id attribute value must be unique within the HTML document).
  • The id attribute can be used to point to a style in a style sheet.
  • The id attribute can also be used by a JavaScript (via the HTML DOM) to make changes to the HTML element with the specific id.