Skip to content

In jsfiddle, it works in pure javascript, But – when I include this in my code, it doesn’t work without jquery included [closed] – Where i get the code.

My code:

const chk = document.getElementById('chk');
const body = document.body;
    chk.addEventListener('change', () => {
        localStorage.setItem("blockIsActive", $('.body').hasClass('dark'));
    var blockIsActive = localStorage.getItem("blockIsActive") 
    if (blockIsActive == "true") {

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?



If you check the Resources tab of that fiddle, it actually says it includes jQuery:

Screenshot of Fiddle meta

Mind that $ isn’t standard JavaScript, but a jQuery function/API to start with.