Dynamically Close Navigation on Small Screens
Explore our JavaScript category, where you’ll find loads of helpful articles and tutorials for everyone, whether you’re just starting out or a pro developer. Learn about different JavaScript functions that can be used in various projects according to their specific requirements.
This code checks if the document’s ready state is “interactive” and if the window’s inner width is less than or equal to 800 pixels. If both conditions are true, it performs some actions:
document.onreadystatechange = () => {
if (document.readyState === “interactive”) {
if (window.innerWidth <= 800) {
document.getElementsByClassName(‘navList’)[0].classList.remove(“is-open”);
document.getElementsByClassName(‘sidebarBlock-heading’)[0].classList.remove(“is-open”);
}
}
}
1- It selects the element with the class ‘navList’ using getElementsByClassName and removes the “is-open” class from it.
2- It selects the element with the class ‘sidebarBlock-heading’ using getElementsByClassName and removes the “is-open” class from it.