'How to target an element using multiple class names with Javascript?
I am working in Wordpress and I was able to target an element to change some of the CSS with this exact code:
body.admin-bar .mobmenu{
top:140px;
}
I need to also target this exact element with Javascript to change it's CSS properties after the user has scrolled down the page to reset it back to: top:0px
How would I target this using Javascript since it's multiple class names? I can't assign it an ID
Solution 1:[1]
Assuming this is the only element with this classname.
You can use the document query selector
const myElement = document.querySelector("body.admin-bar.mobmenu")
Should do the trick
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | zeehyt |
