0

I am using two buttons to toggle css classes that either show or hide a background. I would like to save the current state of each class in local storage and display (or not display) them when the user returns. I have made an attempt with the wrapping class in this jsFiddle, but I don't have it working yet:

http://jsfiddle.net/eqrk6fak/1/

Setting it like so:

    $("#switch2").click(function() {        
    wrap.fadeToggle(600);
    if($('div').hasClass(wrap)){
    localStorage.setItem('background', wrap);
    }
});

And then trying to get it when the user returns:

$(document).ready(function(){
var background = localStorage.getItem('background');  
if(background !== ''){      
    $('#wraping').addClass(background);
}
});

Any help would be appreciated. Thanks!

1 Answer 1

1

The problem is in the following line:

$('div').hasClass(wrap)

Reviewing your JSFiddle code, at that point wrap is a jQuery result:

var wrap = $(".wraping");

According to the documentation, jquery.hasClass receives a className which should be a String.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.