I am a photographer with a limit knowledge of HTML/CSS and almost no knowledge of javascript. I am trying to build my own own website and I wanted to have an option to switch between light/dark background, for a better viewing experience when looking at my pictures. I have found a code to swap between CSS (light/dark).
HEAD HTML:
<link rel="stylesheet" id="pagestyle" type="text/css" href="CSS/light.css" >
<link rel="alternate stylesheet" tittle="dark" type="text/css" href="CSS/dark.css" >
<script src="scripts/styleswitcher.js" type="text/javascript"></script>
BODY HTML:
<button id="light"> Light </button>
<button id="dark"> Dark </button>
And I'm trying to use the javascript code found here. Javascript:
var setdark = function () {
$('#head').append('<link rel="stylesheet" href="CSS/dark.css" type="text/css" />');
},
setlight = function () {
$('link[rel=stylesheet][href="CSS/dark.css type="text/css"]').remove();
};
$("#light").click(function () {
localStorage.setItem('color', 'light');
setlight();
});
$("#dark").click(function () {
localStorage.setItem('color', 'dark');
setdark();
});
if (localStorage.getItem('color') == 'light') {
setlight();
}
else if (localStorage.getItem('color') == 'dark') {
setdark();
}
I edited the code in the hope I can make it work on my website. But it isn't working. What am I doing wrong?