Reading the link below, I’m guessing it doesn’t exist because it doesn’t need to. An element can only have one <id> attribute, whereas it could have multiple classes associated with it. classList will return a comma separated list of all classes on an element.
I believe the fundamental to focus on here is the fact that ids are unique, there is only one per page. Hence, you should never need to look for body#logged-in (if put a timer on that + queryselector, you will also see it is much less performant than getElementById), you should only have to look for that one id ‘logged-in’.
To determine if user is logged in, I would definitely used a class on the body, since it is a state, it’s not identifying anything. Or a fancier way might be to have a data attribute: data-user=“{logged-in: true, email: some@one.com}” and get that via JSON.parse(document.body.getAttribute(‘data-user’))… good way
But that only works, if the ID only ever appears on . If it for whatever reason appears somewhere else, it gives an invalid result.
Exactly! There should never be duplicate IDs on the page. a) it won’t validate on w3c, penalizing your SEO and b) causes issues like this.
I worked a large e-comm company, lots of devs, and this was something we faced a lot. Finally, we had to make a standard to make 100% sure that ID didn’t exist on the page already, and that they are specific to a certain idea.