Use case: I'd like to dynamically add classes to the <html> element in my document so that some SASS mixins that are currently using .class-name & will work if applied within a selector which includes a body class -- otherwise I get .class-name .body-class .rest-of-selector which doesn't work unless class-name is applied at a higher level than the <body>
Previously I've used document.body.classList.add which seems neat, simple and robust.
In transitioning to add classes to the <html> element instead, I wondered what the equivalent code would be:
document.html-- nope, no such thing it seemsdocument.rootElement-- should work I think, but wasnullwhen I tested so not robust?!document.getElementsByTagName('html')[0]-- pretty clunky, but surely effective/robust?document.body.parentElement-- looks cleaner, should be reliable? Is this as direct as it gets?document.querySelector('html')as suggested by Louys below -- feels like we shouldn't need to query for it though?- any other options I haven't thought of?
document.querySelector("html");)document.childNodes[1]because it is supposed to be the doctype and the html nodedocument.documentElementthat give you directly the html node