const toggleDarkOrLight = document.getElementsByTagName('i')[0];
var toggled = false;
const toggle = () => {
if (toggled === false) {
toggleDarkOrLight.classList.remove('fa fa-toggle-off');
toggleDarkOrLight.classList.add('fa fa-toggle-on');
} else {
toggleDarkOrLight.classList.remove('fa fa-toggle-on');
toggleDarkOrLight.classList.add('fa fa-toggle-off');
}
}
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<i class="fa fa-toggle-off" onclick="toggle()"></i>
</body>
<html>
Why can't I change the class? Is it because of the "-" character
What should I try instead then?
Also, how can I make the toggle on and off like a smooth animation (or I can't?)