Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Q.: ID= versus NAME= attributes for A tag

5 views
Skip to first unread message

tlvp

unread,
Jan 31, 2017, 9:19:59 PM1/31/17
to
I used to think the attributes name= and id= were pretty much
interchangeable when used with, e.g., the <A> tag.

And I'd learned that the single digit "1" was an invalid value when used
with either of those attributes. So, being in the habit of wishing to use
the integers 1, 2, ..., 9, ... as such values to help mark locations a TOC
gives links to, as with <A href="#3"> to <A name="3">, I've learned to
substitute "s1" (I remember it as an obligatory "/section/ 1") for "1".

No such validation problem with the simple integers "2", "3", and later,
when used as values for name=" ".

But while name="2", name="3", and so on. seem to pass muster, the analogous
id="2" (or id="3", &c.) doesn't. Quoth the validator(4.01 Transitional
mode) of id="2": *value of attribute "ID" invalid: "2" cannot start a name*

Makes me wonder in what other ways the attributes NAME and ID also differ?
And: how much of a deal-breaker is the invalid use of name="1" or id="2"?

I welcome all instruction on these questions. Thank you. Cheers, -- tlvp
--
Avant de repondre, jeter la poubelle, SVP.

Eli the Bearded

unread,
Feb 1, 2017, 1:08:54 AM2/1/17
to
In comp.infosystems.www.authoring.html,
tlvp <mPiOsUcB...@att.net> wrote:
> Makes me wonder in what other ways the attributes NAME and ID also differ?

ID is the identifier for CSS styling. NAME is the identifier for
scrolling.

> And: how much of a deal-breaker is the invalid use of name="1" or id="2"?

"Be conservative in what you do, be liberal in what you accept from
others." (Also called Postel's Law or the "robustness principle".)

Elijah
------
hasn't tested all numeric ids

Jukka K. Korpela

unread,
Feb 1, 2017, 2:35:23 AM2/1/17
to
1.2.2017, 4:19, tlvp wrote:

> I used to think the attributes name= and id= were pretty much
> interchangeable when used with, e.g., the <A> tag.

They can both be used to set up a destination for links, but they are
not interchangeable. In addition to differences in the syntax of allowed
values, they need different selectors when you wish to use them in CSS
to refer to the element (.name versus #name), for example.

The name= attribute is older, and some people adopted the habit of using
it even after the id= attribute had been introduced, but first not
universally supported. It’s time to abandon such habits, and HTML5
explicitly obsoletes the name= attribute (for <a>; not for form fields,
where it has a completely different role and is indispensable), in favor
of the id= attribute.

The great advantage of the id= attribute is that you can use it on any
element. You don’t need to write

<h2><a name="ch1">Chapter 1</a></h2>

but more naturally

<h2 id="1">Chapter</h2>

or even

<section id="1">
<h2>Chapter</h2>
Content of chapter 1.
</section>

> And I'd learned that the single digit "1" was an invalid value when used
> with either of those attributes.

This was always just a formal constraint, and it has been relaxed in
HTML5, where an id attribute value can be almost anything: it just needs
to contain at least one character and must not contain any space characters.

> So, being in the habit of wishing to use
> the integers 1, 2, ..., 9, ... as such values to help mark locations a TOC
> gives links to, as with <A href="#3"> to <A name="3">, I've learned to
> substitute "s1" (I remember it as an obligatory "/section/ 1") for "1".

That’s what I have used, and it might still be a good idea to use s1 or
even section-1 for legibility of code. Not just HTML, but also CSS. If
you wish to style the element in CSS, you cannot use the selector #3
(but you would need to use a confusing-looking escape: #\33).

> And: how much of a deal-breaker is the invalid use of name="1" or id="2"?

There are no problems with them in browsers. But you may have problems
with validators or checkers.

--
Yucca, http://www.cs.tut.fi/~jkorpela/

Ed Mullen

unread,
Feb 1, 2017, 10:18:14 AM2/1/17
to
On 1/31/2017 at 9:19 PM, tlvp's prodigious digits fired off with great
aplomb:
In addition to the other comments, an id can only be used once in a
single document.

--
Ed Mullen
http://edmullen.net/
On the keyboard of life, always keep one finger on the escape key.

Barry Margolin

unread,
Feb 1, 2017, 11:36:00 AM2/1/17
to
In article <o6s30i$s8l$1...@dont-email.me>,
"Jukka K. Korpela" <jkor...@cs.tut.fi> wrote:

> 1.2.2017, 4:19, tlvp wrote:
>
> > I used to think the attributes name= and id= were pretty much
> > interchangeable when used with, e.g., the <A> tag.
>
> They can both be used to set up a destination for links, but they are
> not interchangeable. In addition to differences in the syntax of allowed
> values, they need different selectors when you wish to use them in CSS
> to refer to the element (.name versus #name), for example.

.name is for class="name", not name="name".

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***

Jukka K. Korpela

unread,
Feb 1, 2017, 12:24:15 PM2/1/17
to
1.2.2017, 18:35, Barry Margolin wrote:

> In article <o6s30i$s8l$1...@dont-email.me>,
> "Jukka K. Korpela" <jkor...@cs.tut.fi> wrote:
>
>> 1.2.2017, 4:19, tlvp wrote:
>>
>>> I used to think the attributes name= and id= were pretty much
>>> interchangeable when used with, e.g., the <A> tag.
>>
>> They can both be used to set up a destination for links, but they are
>> not interchangeable. In addition to differences in the syntax of allowed
>> values, they need different selectors when you wish to use them in CSS
>> to refer to the element (.name versus #name), for example.
>
> .name is for class="name", not name="name".

Thanks for the correction. I don’t know what I was thinking about.

To refer to an element with name="foo" in CSS, you would need the
selector [name="foo"]. Or, to be on the safer side, a[name="foo"], if
it’s an <a> element. (A document may contain other elements with
name="foo", e.g. form fields.)

--
Yucca, http://www.cs.tut.fi/~jkorpela/

tlvp

unread,
Feb 2, 2017, 2:16:20 AM2/2/17
to
It's a welcome and refreshing, yet recurrent, surprise to me, to see how
much more I can learn from the focused comments of knowledgeable people,
such as all of you here, than from the unstructured massed information an
encyclopedic source like Google can provide.

So thank you all very much, then, Eli, Jukka, Barry, Ed, et al.

Helmut Richter

unread,
Feb 2, 2017, 3:56:17 AM2/2/17
to
Am 01.02.2017 um 16:18 schrieb Ed Mullen:

> In addition to the other comments, an id can only be used once in a
> single document.

In this specific context, a name used more than once in a <a
name="name_x"> tag would not make sense either.

--
Helmut Richter



Jukka K. Korpela

unread,
Feb 2, 2017, 4:57:06 AM2/2/17
to
The specifications have always had a uniqueness requirement on such
names. However, violations of this requirement are not detected by
SGML-based validators (since such requirements are not describable in a
DTD) or even by the W3C checker (which is more surprising).

This is yet another reason for using id=... instead of name=... Unique
requirements are checked for id=... attribute values.

--
Yucca, http://www.cs.tut.fi/~jkorpela/
0 new messages