george george - 2 months ago 12
HTML Question

javascript storage class for reloading page

Hi is it possible to add class to element and storage it in element if the page reloads ?

<button></button>
<div class=""></div>

$(document).ready(function () {

$("button").click(function() {
$("div").addClass("test");
});

});

Answer

try

$(document).ready(function () {
  $("button").click(function() {
   $("div").addClass("test");
   localStorage['testClass'] = 'test';
  });  
  previousTestClass = localStorage['testClass'];
  if (previousTestClass) 
  $('div').addClass(previousTestClass); 
});