-2

I have so many children, in an element, So to avoid of maintaining multiple ids, I have used same ids

ex
<div id="1" class="userstatus">
   <div class="floatl userdetails">
    <span id="name" class="ellips">aravi</span>
   </div>
   <div id="scode" class="status status-1"></div>
</div>
<div id="2" class="userstatus">
   <div class="floatl userdetails">
    <span id="name" class="ellips">aravi</span>
   </div>
   <div id="scode" class="status status-1"></div>
</div>

Here I have duplicated name, scode ids for innerElements of 1, 2. I can able to

I can able to get the name by using querySelector api. am I doing right way?

raky
  • 181
  • 1
  • 2
  • 11
  • 4
    Classes are what you need. IDs are unique, duplicate IDs are wrong and should not be used. – SeinopSys Apr 09 '14 at 09:05
  • @DJDavid98 I already mentioned, that querySelector api to find the element. So that I am asking – raky Apr 09 '14 at 09:06
  • If you want to retrieve elements using a querySelector, you can use a particular class name and select them with `.name`, `.scode`, etc. There is never a good excuse to use duplicate IDs. – JLRishe Apr 09 '14 at 09:08
  • Is it good practice ? NO ! Should that be a pactice ? NEITHER. Also, although HTML5 is ok with id's being 1 single digit, I don't think it's wise to do so... – Laurent S. Apr 09 '14 at 09:09
  • @Teemu > indeed I had overlooked that HTML 5 is more permissive. I anyway find it a bad idea to use a single number as id, but that's technically correct so I will update my remark. – Laurent S. Apr 09 '14 at 09:13

2 Answers2

5

You should never use duplicate ids - id should be unique per document.

If you need multiple elements with the same "tag" use classes.

I would recommend you read: http://css-tricks.com/the-difference-between-id-and-class/

Think of it like:

You have on your ID card an id number that is unique to you, no one else has it.

But you are part of the class of people who post Questions on SO, and obviously there are more of us.

Matyas
  • 13,473
  • 3
  • 60
  • 73
0

By definition ID's should describe one element in the DOM uniquely. Also, semantics is compromisied by using multiple ID's.

riccardolardi
  • 1,713
  • 5
  • 20
  • 35