16

Is it safe to give several elements the same ID in one page? For example this often happens, when using some jquery plugins, when you run some slider or gallery twice or more. We know, developers like to give some ID to the html container in order the script works faster.

Let's read w3.org documentation:

What makes attributes of type ID special is that no two such attributes can have the same value; whatever the document language, an ID attribute can be used to uniquely identify its element.

But the next example with 2 elements having the same ID works fine in all browsers, though it's not valid:

#red {
  color: red;
}
<p id="red">I am a red text.</p>
<p id="red">I am a red text too.</p>

Can anybody explain this strange situation?

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Arsen K.
  • 5,494
  • 7
  • 38
  • 51
  • 23
    It has been observed that having multiple elements on one page with identical `ID` attributes for long periods of time will cause spontaneous combustion and nuclear war. If you are caught using multiple identical IDs the internet militia will shoot you on sight. It is about the least safe thing you can do. – zzzzBov Aug 31 '11 at 19:23
  • 5
    I can change the code once per month so they couldn't find me :) – Arsen K. Aug 31 '11 at 19:43
  • It's not clear from your question whether you mean multiple ID selectors in CSS, or multiple copies of the same ID in an HTML page. In fact, you seem to be asking two entirely different questions here. – BoltClock Jan 06 '12 at 04:41
  • Why is it not clear? In my example I wrote 2 paragraph tags with the same id. So I meant "multiple copies of the same ID in an HTML page". – Arsen K. Jan 06 '12 at 22:23
  • 1
    Your question title said "Several ID selectors", so that was potentially a little confusing. I've fixed it for you. I've also expanded my answer for completeness. – BoltClock Jan 07 '12 at 03:06

3 Answers3

35

Browsers always try to "fail silently". What this means is that even though your HTML is invalid, the browser will try to guess what your intent was, and handle it accordingly.

However, deviating from the spec can cause some very unforeseen side effects.

For example:

document.getElementById('red');

will only get you the first one.

You'll also have to test your page in all the browsers that your users might use, to make sure your code works as intended. You can't just assume that it'll work.

In short: Don't do this! If you need to target several elements with the same CSS, use a class name. That's what they were designed for...


Having said that; if you really need to select multiple elements with the same ID, use an attribute selector:

document.querySelectorAll('p[id="red"]');

Note however, that this doesn't work in IE7 and below...

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • 1
    Of course, I won't do this even in my worst nightmare. :) And when I wrote "all browser", I meant 5 major browsers: FF, Opera, Chrome, Safari, IE. – Arsen K. Aug 31 '11 at 19:40
  • 2
    @Webars: expand that to ... IE6, IE7, IE8, IE9, IE10 ;) – Joseph Silber Aug 31 '11 at 20:05
  • 1
    As a quickie, `document.querySelectorAll('p#id')` will in fact behave the same as `p#id` in CSS, because it uses a browser's native CSS selector implementation. Of course, it still remains that you shouldn't ever assign multiple elements the same ID. – BoltClock Oct 25 '12 at 14:01
7

Is it safe to give several elements the same ID in one page?

As you know, it's against the validation rules to give more than one element the same ID in a single page. However, rules are broken, and in the world of HTML tag soup, browsers have to account for these broken rules without breaking pages, hence the behavior you observe.

Although browsers have been shown to behave the same way even if you do so (fortunately for situations where it can't be helped), I wouldn't call it totally "safe" to do so, as such behavior may not be consistent or reliable.

The best bet is to follow the rules as faithfully as you can, and only break the rules if you have very, very good reasons to (and you almost never will, so don't even consider it). Otherwise, like Joseph Silber said, use classes, designed specifically for classifying or grouping multiple elements.

Can anybody explain this strange situation?

The uniqueness of IDs in an HTML document is not enforced or assumed by CSS; instead, the Selectors spec simply states this:

An ID selector represents an element instance that has an identifier that matches the identifier in the ID selector.

Notice the use of the word "an" throughout this sentence.

Following that statement are some example uses, which use the word "any" instead:

The following ID selector represents any element whose ID-typed attribute has the value "chapter1":

#chapter1

The following selector represents any element whose ID-typed attribute has the value "z98y".

*#z98y

The assumption of a conformant document is clarified by level 3 of the Selectors spec, near the beginning of the section (emphasis mine):

What makes attributes of type ID special is that no two such attributes can have the same value in a conformant document, regardless of the type of the elements that carry them; whatever the document language, an ID typed attribute can be used to uniquely identify its element.

Where "conformant" refers to conformance to HTML, not CSS. Keep in mind that this text wasn't present in the level 2 spec, which is the one you quote in your question.

Bear in mind that the text that is quoted is not normative. While it's a way of helping implementers work out error-handling cases, it's not a compulsory rule to be followed, and indeed, any implementation is free to behave differently without being in violation of the spec. Don't write invalid HTML just to take advantage of what may seem to be expected or consistent behaviors, because you can't guarantee that these behaviors will remain that way. Any CSS implementation is free to match elements sharing the same ID differently, or even stop matching them altogether, if it decides that's how it should handle documents that break this rule.

In other words: just because you can, doesn't mean you should.

Community
  • 1
  • 1
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
0

This works with simple HTML styling but will not work with queries.

From the jQuery API documentation:

Each id value must be used only once within a document. If more than one element has been assigned the same ID, queries that use that ID will only select the first matched element in the DOM. This behavior should not be relied on, however; a document with more than one element using the same ID is invalid.

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
Rupert Horlick
  • 671
  • 5
  • 15