I'm trying to make a website which can switch to dark mode when user wants to. I made 2 stylesheets 1. style.css and 2. darkstyle.css
To make switching possible, I took the code from:JS fiddle and DevelopPHP and merged them accordingly to make the following code:
HTML:
<head>
<title>Home</title>
<link id="pagestyle" rel="stylesheet" type="text/css" href="style.css">
<script type="text/javascript" src="changeCSS.js"></script>
</head>
<body>
<label for="mode"><input id="mode" type="checkbox"> Dark Mode</label>
...
</body>
The JavaScript is
$('#mode').change(function(){
if ($(this).prop('checked'))
{
document.getElementById('pagestyle').setAttribute('href', "darkstyle.css");
}
else
{
document.getElementById('pagestyle').setAttribute('href', "style.css");
}
});
The code above doesn't work.
PS: I searched a lot about this and found a stackoverflow answer but I can't integrate this answer with a checkbox.