I have a set of items that get saved but I'm trying to change a value after I retrieve it and before I pass it of to my php function. Is it possible to change just one item?
My string of items looks like this:
var args = window.localStorage.getItem("localusers");
start=0&type=1
I'm saving the localusers item like so:
window.localStorage.setItem('localusers', $("#profile_form_id").serialize());
I'm trying to set the value of type to a 6
I can't find a supported method that allows me to get/set an item in the array so do I need to iterate through the array, change the value and then set the localusers item again? It doesn't seem very efficient but I can't see any other way.
Any thoughts or suggestions?
localstorage
does not return an array or an object. It can only store a string.The way people get around it, is to
JSON.stringify()
the array/object before passing it tosetItem
. Then, when retrieving it withgetItem()
, you runJSON.parse()
on it.Assuming this is an object, and you've parsed it, you can modify your
args
variable before you send it along:To ease all this, here's a little helper that you can call:
If you include this script, you can then set and get items through
localstorage.get
andlocalstorage.set
, and it'll automatically convert it to & from JSON.Try this
Like other
JavaScript
objects, you can treat thelocalStorage
object as an associative array. Instead of using thegetItem()
andsetItem()
methods, you can simply use square brackets.