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/