https://jsfiddle.net/c2o4j8fz/1/ – Where i get the code.
My code:
JavaScript
x
13
13
1
const chk = document.getElementById('chk');
2
const body = document.body;
3
$(function(){
4
chk.addEventListener('change', () => {
5
$('.body').toggleClass('dark');
6
localStorage.setItem("blockIsActive", $('.body').hasClass('dark'));
7
})
8
var blockIsActive = localStorage.getItem("blockIsActive")
9
if (blockIsActive == "true") {
10
$('.body').addClass('dark');
11
}
12
});
13
My code displays this error $ is not defined in the console, until i add jQuery, but in the jsfiddle example, it works in pure js. What am I doing wrong?
Advertisement
Answer
If you check the Resources tab of that fiddle, it actually says it includes jQuery:
Mind that $
isn’t standard JavaScript, but a jQuery function/API to start with.