-3

There is a written (several times) rule that sais that an #id selector identifies an unique element into a DOM tree.

This is always followed by the conclusion that only one alone element with this #id can exist into the DOM tree.

So if you need to use a CSS selector for a kind of element that has another occurrences into the DOM tree you have to use a .class selector.

Well, I have serios doubts about this constriction also I think is not responding to a real need and avoiding a more intuitive use of the #id and .class selectors.

The question:

Existing the chain selectors like '#id1 #id2' I could understand that #id2 better to be unique into the #id1 context, but which is the meaning of forcing that #id2 should be unique into the whole DOM context?

This rule is just fueling a degenerative hell of id nonsense-naming. Also forcing us to over use the class attribute.

I feel more confortable avoiding this rule and building structures like this, (please understand that the code is simplified and not using concrete HTML structures in propose):

<div id="armys" class="list">
  <div id="1" class="element">
    <div id="orders" class="text">My orders</div>
    <div id="soldiers" class="list">
      <div id="1" class="element">
        <div id="name" class="text">John</div>
      </div>
      <div id="2" class="element">
        <div id="name" class="text">Esther</div>
      </div>
    </div>
  <div>

  <div id="2" class="element">
    <div id="orders" class="text">My orders</div>
    <div id="soldiers" class="list">
      <div id="3" class="element">
        <div id="name" class="text">Smith</div>
      </div>
    </div>
  <div>
</div>

Which is a very much natural translation of an structure like this:

armys: [
  {
    id: 1,
    orders: "My orders",
    soldiers: [
      {
        id: 1,
        name: "John"
      },
      {
        id: 2,
        name: "Esther"
      }
    ]
  },
  {
    id: 2,
    orders: "My orders",
    soldiers: [
      {
        id: 3,
        name: "Smith"
      }
    ]
  }
]
fguillen
  • 36,125
  • 23
  • 149
  • 210
  • 4
    This isn't a question so much as a rant. To have valid HTML, you need to use unique identifiers. That's the way it was defined. – zzzzBov Mar 15 '12 at 16:47
  • It's not a restriction. It's just axiom. What do you mean with "overuse" of class attribute? Do you know that one element can have multiple classes (space-separated in value of `class` attribute) at the same time? – Marat Tanalin Mar 15 '12 at 16:49
  • @zzzzBov the question is: _which is the meaning of forcing that #id2 should be unique into the whole DOM context?_ – fguillen Mar 15 '12 at 16:57
  • @MaratTanalin as I can't have multiple _id="soldiers"_ in the same page I have to move this classification to the _class_ attribute, when for me it is a clear unique element for the "#armys #1" element and not a "kind of..." as I see the _class_ attribute. – fguillen Mar 15 '12 at 17:00
  • @fguillen, you should read the [HTML spec](http://dev.w3.org/html5/spec/global-attributes.html#the-id-attribute), because it seems you don't understand the underlying rules. – zzzzBov Mar 15 '12 at 17:04
  • Your `soldiers` elements are a class of element, a number of elements containing soldiers. `
    ` (using an arbitrary unique `id` to denote a particular army's soldiers)
    – Andrew Leach Mar 15 '12 at 17:05
  • @fguillen: Talking this way, each element is unique, and classes are unneeded at all which is not truth. You not can't. You just shouldn't. – Marat Tanalin Mar 15 '12 at 17:05
  • @zzzzBov you are very right I just realized that what is exactly saying is _"The value must be unique amongst all the IDs in the element's home subtree"_ and what I understand with [home subtree](http://dev.w3.org/html5/spec/infrastructure.html#home-subtree) is the context of the first ancestor of the element. So I think I was confused, and I think I wasn't the unique, and what the rule really is is: **"An ID has to be unique in amongst all the elements with the same parent"**. If this is true is gonna be a shutdown discussions. – fguillen Mar 15 '12 at 17:24
  • you know, when i was first learning html, css etc, using the validator really helped me understand what you can and can't do. because the HTML spec is one thing, but each browser implements things a little differently. validate your pages here: http://validator.w3.org/ and see what the w3c tells you about it. – Chris Drappier Mar 15 '12 at 18:52
  • I can't figure out how best to edit your question other than by removing more than half of it, which I imagine isn't something you'd like me to do. You're in a better position to work out a more straightforward and less confusing/ranty/rhetorical way to put forth your question. – BoltClock Mar 15 '12 at 19:42
  • FWIW, CSS does not care that an ID in an HTML document (or any other document) is unique. See [this answer](http://stackoverflow.com/a/8753329/106224) and [the comment thread under this question](http://stackoverflow.com/questions/5797014/css-selectors-parsed-right-to-left-why). – BoltClock Mar 15 '12 at 19:44

1 Answers1

1

You aren't approaching this correctly. The class and ID attributes aren't meant for CSS, nor for JavaScript. They are meant to IDentify and CLASSify the different elements.

Unique elements get an ID, that has merits in CSS and JavaScript, as well as native browser behavior (Jump to the ID if the matching anchor link is clicked).

Elements similar in meaning get a shared class name. This helps classify them in CSS and JavaScript (HELPS!), to have a better grouping of similar elements.

Also, FYI, there are different elements that represent unordered lists and list items, so that you won't have a page filled in unnecessary divs.

Madara's Ghost
  • 172,118
  • 50
  • 264
  • 308
  • I have not talked about CSS but CSS selectors **.** Anchor links match with 'name' not 'id' **.** I don't see the constraint of 'whole DOM id uniqueness' a help for classify, especially having the 'chain selectors' **.** I already have said that 'I am simplifying my example in propose' – fguillen Mar 15 '12 at 16:56
  • @fguillen, fragment identifier links (i.e. `foo`) are defined as going to the element with a particular `[id]` in the [HTML spec](http://dev.w3.org/html5/spec/history.html#scroll-to-fragid) – zzzzBov Mar 15 '12 at 17:02
  • @zzzzBov you are right I misunderstood the specifications with this [W3 org about anchor](http://www.w3.org/MarkUp/1995-archive/Elements/A.html) _"the attribute NAME allows the anchor to be the destination of a link"_ – fguillen Mar 15 '12 at 17:19
  • @fguillen, that's an archive from 1995...Much has changed since then. – zzzzBov Mar 15 '12 at 17:21