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 please
15 Replies
ἔρως
ἔρως4mo ago
no, because that is a single id
Jochem
Jochem4mo ago
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.
Faker
FakerOP4mo ago
ah ok I see Now I understand why we can't use JS to append new ids but with class we can
ἔρως
ἔρως4mo ago
oh, yeah, forgot whitespaces arent liked on ids
Faker
FakerOP4mo ago
make sense now, thanks !! 💯
ἔρως
ἔρως4mo ago
why would you do what? sounds like a job for a data attribute anyways
Faker
FakerOP4mo ago
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" ?
ἔρως
ἔρως4mo ago
you can "add" by just doing element.id += 'something'; but the id then is changed
Faker
FakerOP4mo ago
yep
ἔρως
ἔρως4mo ago
ids dont work like classes, as classes are supposed to be a list
Faker
FakerOP4mo ago
it overwrites it
ἔρως
ἔρως4mo ago
the id is supposed to uniquely identify a single element in the entire document
Faker
FakerOP4mo ago
yep I see
ἔρως
ἔρως4mo ago
what you can do is use a data-id and shove json into it, but selecting the values in css will be absolute hell
Faker
FakerOP4mo ago
yeah I see what you mean it's good to know

Did you find this page helpful?