Simmons University



The name vs. id attribute in HTML5If an element has both a name and an id attribute they should be the same (except where the name is shared by multiple elements, such as in a set of radio buttons).As explained at the name and id attributes are different.The name attribute is for submitting a form element to the server; many elements may share the same name (e.g. radio buttons, which must have the same name within the set).The id attribute is for uniquely identifying any element (not just form elements). It must be unique throughout the entire document.To expand on this explanation:If you have a form which will be submitted to a server, then the data in that form will be sent in key-value pairs. The key will be the name of the form element, so any form element which has data to submit must have a name.If you wish to identify an element in a form or another part of your HTML page, then you need to use an id attribute.While a name is sometimes re-used (as in radio buttons), an id is unique and may be used only once on a page. This also allows you to get ahold of that element by using the function document.getElementById().Any element may have an id but you should use the name attribute only for forms and for backwards compatibility with old (HTML4.01) browsers. Compatibility with HTML4.01browsers is becoming increasingly unimportant.At the w3c gives the following code example and explanation:(see next page)In this example, a form contains a text field and a submission button:<form action="addcomment.cgi" method=post> <p><label>Comment: <input type=text name="comment" dirname="comment.dir" required></label></p> <p><button name="mode" type=submit value="add">Post Comment</button></p></form>When the user submits the form, the user agent includes three fields, one called "comment", one called "comment.dir", and one called "mode"; so if the user types "Hello", the submission body might be something like:comment=Hello&comment.dir=ltr&mode=addMore details for those who want them:Theoretically the name attribute is being deprecated, starting with HTML5 (except for control elements in forms). The w3c says: ATTRIBUTE SHOULD NOT BE SPECIFIED ON <a ?> elements, but if it is there then you must use the same value as on the id element.Authors should not specify the?name?attribute on?a?elements. If the attribute is present, its value must not be the empty string and must neither be equal to the value of any of theIDs?in the element's?home subtree?other than the element's own?ID, if any, nor be equal to the value of any of the other?name?attributes on?a?elements in the element's?home subtree. If this attribute is present and the element has an?ID, then the attribute's value must be equal to the element's?ID. In earlier versions of the language, this attribute was intended as a way to specify possible targets for fragment identifiers in?URLs. The?id?attribute should be used instead.So what are the implications? First of all you should understand that names and ids share the same namespace, so you need to be sure there are no conflicts. Secondly, there is good news and bad news:?THE BAD NEWSStarting with HTML5 the w3c is deprecating the name attribute. ?That is <a name='foo' /><a href='#foo'> is going to be gone!*THE GOOD NEWS*You can link to ANYTHING which has an id - e.g. a paragraph, a form etc.So<h3 id ='contactInfo'>Contact information</h3><a href='#contactInfo'>Want more information?</a>*MEANWHILE*Since some browsers (notably IE8 and earlier, Firefox 3.4 and earlier) don't yet "do" HTML5, the safest way ?is<a name='foo' id='foo' /> <a href='#foo'>More nonsense?</a>Of course, when an element has both aname and an id they must be the same.It's not clear to me what will happen to the anchors[] array.? ? ?12.2.3 Anchors with the id attributeThe id <; attribute may be used to create an anchor at the start tag of any element (including the A <; element).This example illustrates the use of the id <; attribute to position an anchor in an H2 <; element. The anchor is linked to via the A <; element.You may read more about this in<A href="#section2">Section Two</A>./...later in the document/<H2 id="section2">Section Two</H2>/...later in the document/<P>Please refer to<A href="#section2">Section Two</A> ?abovefor more details.The following example names a destination anchor with the id <; attribute:I just returned from vacation! Here's a<A id="anchor-two">photo of my family at the lake.</A>.The id <; and name <; attributes share the same name space. This means that they cannot both define an anchor with the same name in the same document. It is permissible to use both attributes to specify an element's unique identifier for the following elements: A <;, APPLET <;, FORM <;, FRAME <;, IFRAME <;, IMG <;, and MAP <;. When both attributes are used on a single element, their values must be identical.ILLEGAL EXAMPLE:The following excerpt is illegal HTML since these attributes declare the same name twice in the same document.<A href="#a1">...</A>...<H1 id="a1">/...pages and pages.../<A name="a1"></A>The following example illustrates that id <; and name <; must be the same when both appear in an element's start tag:<P><A name="a1" id="a1" href="#a1">...</A> ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download