Search code examples
javascriptjquerycssuniqueidentifier

Is it normal to have two elements with same id in two div elements with other id?


I know, that two elements can't hav the same id. But it's happens so, that in my project I have two elements with same id in other divs, like this

<div id="div1">
     <img id="loading" />
</div>
<div id="div2">
     <img id="loading" />
</div>

And CSS:

#div1 #loading
{
    some style here...
}
#div2 #loading
{
    another style here...
}

Works fine for me, but maybe it is not reccomended to do by so?

Yes, I know, thet I can use classes, and it's strongly recomended to do by so, but I want to know is there any potential risk in this usage of id? I think no, because when I wrote for example

$("#div1 #loading")... it becomes a unique element. Isn't it?


Solution

  • https://www.w3.org/TR/1999/REC-html401-19991224/struct/global.html#adef-id

    enter image description here

    BUT!

    If you need it in your project, you can use it, like suggested in my Question Final Update!