0

Hi there my knowledge about JS is very amateurish and even that is an understatement but i'm still learning so please bear with me.

Issue:

Im using actually a snippet on a local project from the user sanman here https://stackoverflow.com/a/16303790/7981697

I was able to implement it and it also works but now i'd like to store the collapsed or expanded state via locally stored cookies (i heard that's the most effective way to do so, but feel free to correct me here if i'm wrong).

My actual JS:

$(document).ready(function() { 
var clicked = 0;
$('.sidebar_button').on('click',function(){
if(clicked===0){
collapse();
clicked = 1;    
}
else if(clicked===1){
expand();
clicked = 0;
}           
})
function collapse(){
$('.sidebar').animate({
'width':'25%'
},0);
$('.content').animate({
'width':'75%'
},0);
}
function expand(){
$('.sidebar').animate({
'width':'0%'
},0);
$('.content').animate({
'width':'100%'
},0);
}
});
$(document).ready(function() { 
var clicked = 0;
$('.topbar_button').on('click',function(){
if(clicked===0){
collapse();
clicked = 1;    
}
else if(clicked===1){
expand();
clicked = 0;
}           
})      
function collapse(){
$('.topbar').animate({
'height':'100%'
},0);   
}
function expand(){
$('.topbar').animate({
'height':'0px'
},0);
}
});

I've tried a lot of other threads and suggestions here on stackoverflow but i was not able to implement any of them since the JS structures were completely different so it would help a lot if you could help with direct directions in context with my provided JS snippet or even maybe with a jsfiddle demo since it would be also much easier for me to understand the whole approach.

In the meantime i'll try to solve it by trial and error somehow.

Thank you in advance :)

Community
  • 1
  • 1
Dohn Joe
  • 23
  • 7
  • Possible duplicate of [How do I create and read a value from cookie?](http://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie) – gforce301 May 10 '17 at 16:04

1 Answers1

0

I personally think that Local Storage is better than cookie because you can store everything in it (only strings in cookies), and because it's easier to use. To store your variable, just do localStorage.setItem("name", value); and to access it : localStorage.getItem( "name") More information : https://developer.mozilla.org/en-US/docs/Web/API/Storage/LocalStorage

For more browser compatibility, you can also use window.localStorage.

Ilwan
  • 43
  • 8