/    Sign up×
Community /Pin to ProfileBookmark

Get navigation “close” status

I have a sidenav that opens and close by javascript and css. My problem is that if the navigation is closed, a refresh opens it again.

Open: <div class=”nav”>
Closed: <div class=”nav close”>

“`
hamburger.addEventListener(“click”, menu);
function menu() {
nav.classList.toggle(“close”)
subnav.classList.toggle(“close”)
}
“`

How do I get the status of the menu? Like (pseudocode):

“`
if (nav.Class) == “close”) {
stay closed;
} else {
open;
}
“`

Live: http://94.237.92.101:3030

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumMay 10.2020 — Store the status in local storage. Then you can read it at page load and restore the previous status.
Copy linkTweet thisAlerts:
@sibertauthorMay 11.2020 — > @Sempervivum#1618297 Store the status in local storage. Then you can read it at page load and restore the previous status.

Thanks! But if I use - toggle("close") , how do I fetch the actual "toggled status"? Not the saved status.
Copy linkTweet thisAlerts:
@SempervivumMay 11.2020 — how do I fetch the actual "toggled status"?[/quote]You can query whether the class is set or not:
if (nav.classList.contains("close") {
// nav is closed
} else {
// nav is open
}

However this will not be necessary as you know about the status:
// get previous status from local storage
if (/* previous status was closed */) {
nav.classList.add("close");
} // if status after loading is "open" no removing of the class is necessary
Copy linkTweet thisAlerts:
@sibertauthorMay 11.2020 — Thanks! Finally I got to work (I think). The main code (resizing, DOMContentLoaded etc must also be altered):

``<i>
</i>hamburger.addEventListener("click", menu);
function menu() {
var width = window.innerWidth;
if (localStorage.nav == "close") {
nav.classList.remove("close")
window.localStorage.setItem('nav', "");
if (width &lt; 768) {
subnav.classList.add("close")
window.localStorage.setItem('subnav', "close");
} else {
subnav.classList.remove("close")
window.localStorage.setItem('subnav', "");
}
} else {
nav.classList.add("close")
subnav.classList.add("close")
window.localStorage.setItem('nav', "close");
window.localStorage.setItem('subnav', "close");
}
}<i>
</i>
``

live: http://94.237.92.101:3030/mytopics
×

Success!

Help @sibert spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...