JSFiddle - React, Tailwind, and code Playground

by oduska

HTML

<body>
    <a href="#" class="toggle-menu">Toggle</a>
</body>

CSS

body.pinned { background: red; }

JavaScript

// I want to toggle the body class, save the current toggle value to localStorage then get that value when the page loads

$(function () {
    $(".toggle-menu").click(function () {
        $("body").toggleClass("pinned");
        localStorage.setItem("menuState", $("body").hasClass("pinned") );
    });
     
    var pinned = localStorage.getItem("menuState");
    
    if (pinned == "true") {
        $("body").addClass("pinned");
    }
});