From what I understand, in HTML5 we're supposed to start using the id
attribute to reference a
elements, instead of the name
attribute. The problem with this is that there is also a constraint that id
should be unique per document.
Now, I see a lot of situations where you'd want to reference a set of related a
elements (in JS, typically). By getting rid of the name
attribute though, we're forced to either reuse the same id
, or use a class
name. Classes should supposedly be about presentation, so this is also an imperfect solution if you're referencing your a
's for non-presentation related reasons.
I suppose another HTML5 alternative would be to use something like data-name
, but this also seems hackish. Is there something I'm missing? Is there a secret W3 conspiracy to phase out name
altogether?
Ah, HTML. How inconsistent you are.
The issue, it seems, is the confusion with name
attribute for anchors and the name
attribute for inputs. Back in the pre-HTML5 days, you needed a way to distinguish an outgoing link (one via href
) with an internal document link (a #
bookmark).
So, how would they accomplish this? It's specified here: http://www.w3.org/TR/html401/struct/links.html
By activating these links (by clicking with the mouse, through keyboard input, voice commands, etc.), users may visit these resources. Note that the href attribute in each source anchor specifies the address of the destination anchor with a URI.
The destination anchor of a link may be an element within an HTML document. The destination anchor must be given an anchor name and any URI addressing this anchor must include the name as its fragment identifier.
Destination anchors in HTML documents may be specified either by the A element (naming it with the name attribute), or by any other element (naming with the id attribute).
Yes, give an anchor a name
attribute (which acts as a fragment identifier) which another anchor could link
to.
Only, with HTML5, they decided any element (other than a
) could act as fragment identifier (which makes sense - there should only be one fragment that you can link to...) - Which is why they deprecated the name
attribute.
As for how to select them? The class
attribute is perfectly fine (sure, it's used for styling, but does it make it purely presentational?)
Another quote (from w3c: http://www.w3.org/TR/html401/struct/global.html#adef-class)
The class attribute, on the other hand, assigns one or more class names to an element; the element may be said to belong to these classes. A class name may be shared by several element instances. The class attribute has several roles in HTML:
*As a style sheet selector (when an author wishes to assign style information to a set of elements).
*For general purpose processing by user agents.