0

I want this code to find a local storage value and if it is true show a grid but if it is false, hide the grid. I tried this code but I can't get it to work right. Any ideas on what I did wrong?

var v = localStorage.getItem('checkcalfid'); 
if (v===true)
{
jQuery('[name="mobilegridcell_385"]').closest("tr").show();
}
else jQuery('[name="mobilegridcell_385"]').closest("tr").hide();

2 Answers2

1

Try JSON.parse it could be that the value stored is not a boolean and make sure you use JSON.stringify when you set the item.

// storing value using JSON.stringify
localStorage.getItem("key",JSON.stringify(value));

-------------------------------------------------------------

// retrieving value using JSON.parse
var v = JSON.parse(localStorage.getItem('checkcalfid')); 
if (v===true)
{
jQuery('[name="mobilegridcell_385"]').closest("tr").show();
}
else jQuery('[name="mobilegridcell_385"]').closest("tr").hide();
Adi
  • 5,560
  • 1
  • 24
  • 37
0

The getItem function of localStorage return string if you want your code to work consider change it to.

Here is a jsfiddle example : https://jsfiddle.net/dkabf953/1/

var v = localStorage.getItem('checkcalfid'); 
if (v== 'true') { // Check if it's equal to the string true
    jQuery('[name="mobilegridcell_385"]').closest("tr").show();
} else {
    jQuery('[name="mobilegridcell_385"]').closest("tr").hide();
}
BentoumiTech
  • 1,627
  • 14
  • 21
  • So I have check boxes that determine how the local storage is set. If it is checked, it is true and if it isn't it is false so how would I get it to say if checked set value to true else false. @KhaledBentoumi – Ellen Schlechter Mar 14 '15 at 19:36
  • @EllenSchlechter just edited my code and added a jsFiddle example. Normally you don't have to change anything beside the code that you posted in your answer – BentoumiTech Mar 14 '15 at 19:44