dev
dev

Reputation: 411

What is the usage of the 'name' attribute in H1..H6 tags?

As far as I understand, the id attribute is used as a unique identifier for some tag in the entire document, while name is used as an identifier (not necessarily unique in the document) that is sent with some data to the server.

However, using the element inspector in pages from the MDN (take this as an example), I noticed that all the article's h1..h6 tags that are present in the navigation tree have name attributes, and that they are identical to their respective tag's ids. In this case, what is the usage of such attributes? Are they used to build the navigation tree?

Upvotes: 2

Views: 5416

Answers (2)

Jukka K. Korpela
Jukka K. Korpela

Reputation: 201886

The name attribute is allowed in various elements and has partly different meanings in them. On the page linked to in the question, it is used in heading elements, e.g.

<h2 id="JavaScript_Review" name="JavaScript_Review">JavaScript review</h2>

Such usage is not valid in any HTML version, and the name attribute has no effect in such elements. The construct is probably generated by some authoring software that has been coded or configured oddly.

To be exact, the attribute (like unknown attributes in general) is not completely ignored. It is stored in the DOM, in the attributes object of the element node (but not as name property of the node, as it would be if it were a defined attribute). This means that it could be used in scripting and in styling.

Upvotes: 1

j08691
j08691

Reputation: 208032

Most likely it's used for bookmark anchors to jump to a specific part of the page. In ye olden days, you would use the name attribute to target the element, today however you use the ID. So it's probably just a holdover for backwards compatibility.

If you check out https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Clicking_and_focus, you'll see the explanation:

name (HTML 4 only, Obsolete since HTML5) This attribute is required in an anchor defining a target location within a page. A value for name is similar to a value for the id core attribute and should be an alphanumeric identifier unique to the document. Under the HTML 4.01 specification, id and name both can be used with the <a> element as long as they have identical values. Usage note: This attribute is obsolete in HTML5, use global attribute id instead.

Upvotes: 4

Related Questions