I have js in my header that checks local storage and sets the stylesheet accordingly. I am using a Bootstrap navbar and I want the navbar to change to inverse colour depending on the local storage too but I can't get it to work. I think it is because the navbar code is in the body.
Code in the head section below:
<script>
if (localStorage.getItem("theme") == "light") {
document.getElementById("maincss").href = "./css/main.css";
} else if (localStorage.getItem("theme") == "dark") {
document.getElementById("maincss").href = "./css/dark.css";
} else {
document.getElementById("maincss").href = "./css/main.css";
};
</script>
Code in the body section below:
<nav class="navbar navbar-default navbar-fixed-top" id="navbarid">
I want to change the navbar to navbar inverse using the bootstrap class "navbar-inverse" if localStorage is "dark".
I just cannot work out how to run something on page load that checks the localstorage (as in the head section) and changes the nav class (in the body section).
Any guidance appreciated. Thanks.