I am currently in the process of creating my first website. I have successfully added two buttons that allow users to switch between a light theme and dark theme, with the light theme being the default. However, I am encountering an issue that I cannot seem to solve: whenever I change the theme to dark and click on another page in the menu (I have multiple HTML pages), the theme reverts back to the default one. I would like for users to be able to maintain their chosen CSS sheet throughout their entire navigation of the website. I have read that using localStorage can help achieve this, but despite trying some code snippets I found online, I have been unsuccessful in making it work. Below, I will include the HTML and JS code that I have implemented so far on my website. Please keep in mind that I am new to web programming.
HTML head section:
<link id="pagestyle" rel="stylesheet" type="text/css" href="stylesheetlight.css">
HTML body section:
<div class="themebuttons">
<button onclick="swapStyleSheet('stylesheetlight.css')"><img src="sun.svg"></button>
<button onclick="swapStyleSheet('stylesheetdark.css')"><img src="moon.svg"></button>
</div>
JAVASCRIPT code
function swapStyleSheet(sheet) {
document.getElementById('pagestyle').setAttribute('href', sheet);
}