I am new to jS and trying some simple tasks but even these so-called simple tasks are proving difficult at times. Please help me understand why this doesn't work. This snippet is to change style sheets by using a toggle. As you can probably work out.
I have two style sheets, a toggle (checkbox) and a function with an if else statement. Why can I not get this to work? Thank you
function toggleTheme() {
let theme = document.getElementsByTagName('link')[0];
if (theme.getAttribute('href') == 'darkstyle.css') {
theme.setAttribute('href', 'lightstyle.css');
} else {
theme.setAttribute('href', 'darkstyle.css');
}
}
<link rel="stylesheet" href="darkstyle.css" />
<link rel="stylesheet" href="lightstyle.css" />
<label class="switch">
<input type="checkbox" />
<span onchange="toggleTheme()" class="slider round"></span>
</label>