The name attribute is valid only for <form> and form elements ( <input> , <textarea> and <select> ). He used to indicate name to associate with a name / value pair that is sent to the form message.
For example:
<input type="checkbox" name="foo" value="1" />
if the flag is sent foo=1 . In the DOM, you can reference form elements from the form.elements collection by specifying name as an index. If name not unique, the collection returns an array of elements, not an element. Modern DOM support for finding form elements by name:
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 identifier for any node, not just form elements. Unlike the name attribute, it is valid for any HTML node. Like the name attribute, it must follow the rules of a valid identifier. Certain ones must start with alpha and contain only alpha ( [a-zA-Z] ), numbers, hyphens, underscores, and colons (note: ASP.NET violates this rule by running reserved identifiers with underscores - this way they will always exit building HTML / XML lint - in fact, some proxies share them). To find any HTML element using id , you use:
document.getElementById(idvalue)
this returns only one DOM node.
Kolten Apr 01 '11 at 20:32 2011-04-01 20:32
source share