2

I want to add a class to body in HTML using Javascript using a button that executes a function, then remove it using another button. But I want to save that class, until the other button is pressed, using LocalStorage. I can do that without LocalStorage

$$('body').addClass('Class here');

But how with LocalStorage?

2 Answers 2

1

By using Storage.setItem to save, then Storage.getItem to retrieve, like this:

var className = "theclass";

// Put the class name into storage
localStorage.setItem('className', className);

// Retrieve the class name from storage
var retrievedClassName = localStorage.getItem('className');

$('body').addClass(retrievedClassName);
2
  • What do I put into both functions? Commented Oct 15, 2016 at 12:45
  • setItem(key,value), getItem(key)
    – jrbedard
    Commented Oct 15, 2016 at 12:46
0

Here a working code with jquery:

<html>
    <head>
        <script type="text/javascript" src="http://code.jquery.com/jquery-3.1.1.min.js"></script>
    </head>
    <body class="">
        <div class="form-group">
            <button class="button">your button</button>
        </div>
        <script>
            var className = "theclass";

            jQuery('.button').on( 'click', function(){
                localStorage.setItem('className', className);
                jQuery('body').addClass(className);
            });

            // Retrieve the class name from storage
            var retrievedClassName = localStorage.getItem('className');
            jQuery('body').addClass(retrievedClassName);
        </script>
    </body>
</html>

And here without:

<html>
    <head></head>
    <body id="mybigbody" class="">
        <div class="form-group">
            <button id="button">your button</button>
        </div>
        <script>
            var className = "theclass";
            var body = document.getElementById("mybigbody");

            document.getElementById('button').onclick = function(e) {
                localStorage.setItem('className', className);
                body.setAttribute("class", className);
                e.stopPropagation();
            }

            // Retrieve the class name from storage
            var retrievedClassName = localStorage.getItem('className');
            if(retrievedClassName)
                body.setAttribute("class", retrievedClassName);
        </script>
    </body>
</html>
18
  • I am not using Jquery Commented Oct 15, 2016 at 13:28
  • @jekeajames So tell to me what is the $$? I thinked was a jquery typo.
    – jedi
    Commented Oct 15, 2016 at 13:30
  • Anyway the problem is the same. Inside your click event you can set the classname on your localstorage and on your body. And outside, at start you must retrive your class with getItem and the attach it on body.
    – jedi
    Commented Oct 15, 2016 at 13:31
  • How, by adding getItem to the body? Sorry but I am new to JS Commented Oct 15, 2016 at 13:35
  • @jekeajames with var retrievedClassName = localStorage.getItem('className'); you can get from your local storage the class that you have saved with click in some precedent moment. So on retrievedClassName you have your class name. Now you must attach it to your body. Whit javascript without andy library you can do var body = document.getElementById("body"); body.setAttribute("class", retrievedClassName);
    – jedi
    Commented Oct 15, 2016 at 13:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.