Can we use multiple id attributes in a single element just like we can with classes?
Hello, consider the following:
<div id="first second"></div>
is this valid?
<div class="first second"></div>
We can have something like that though, but for id if it's not valid, is there a reason why we can't do that please15 Replies
no, because that is a single id
no, because IDs are supposed to be unique and can't contain whitespace https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/id
An ID attribute's value must not contain ASCII whitespace characters. Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the class attribute, which allows space-separated values, elements can only have one single ID value.
ah ok I see
Now I understand why we can't use JS to append new ids but with class we can
oh, yeah, forgot whitespaces arent liked on ids
make sense now, thanks !! 💯
why would you do what?
sounds like a job for a data attribute anyways
nan, was just experimenting and I thought of that 😂 ... was looking for the property that add a class but then it came to my mind: "what if I want to add a new id" ?
you can "add" by just doing
element.id += 'something';
but the id then is changedyep
ids dont work like classes, as classes are supposed to be a list
it overwrites it
the id is supposed to uniquely identify a single element in the entire document
yep I see
what you can do is use a data-id and shove json into it, but selecting the values in css will be absolute hell
yeah I see what you mean it's good to know