keep the menu closed after page refresh

Viewed 27

Hello i have a menu when it's open it have a class: left_right_section and when its closed it have two classes: left_right_section and leftpanel_collapse. I need that when the user close the menu and refresh the page or go to another page in the app the menu should still closed, and if it's open it should still opened so i check the local storage if the menu is closed or not but my code is not working:

if (document.querySelector('.leftpanel_collapse') !== null) { 
  localStorage.ClassName = 'leftpanel_collapse';
}
   
function SetClass() {
  // before assigning class check local storage if it has any value
  $('.left_right_section').addClass(localStorage.ClassName);
}

$(document).ready(function() {
  SetClass();
});
1 Answers

You need to use functions getItem() and setItem() to properly use localStorage

localStorage.setItem("ClassName", "leftpanel_collapse");
$('.left_right_section').addClass(localStorage.getItem("ClassName"));
Related