Work with array of objects in browser local storage
Add methods to work with objects in local storage
$(document).ready(function () {
Storage.prototype.setObj = function (key, obj) {
return this.setItem(key, JSON.stringify(obj))
}
Storage.prototype.getObj = function (key) {
return JSON.parse(this.getItem(key))
}
var myObj = [];
localStorage.setObj('myObj', myObj);
});
Save item in local storage
function SaveItem() {
var myObj = localStorage.getObj('myObj');
var objNome = $('#txtFstName').val();
var objApelido = $('#txtLastName').val();
var obj = { 'fstName': objNome, 'lastName': objApelido, 'age': 32 };
myObj.push(obj);
localStorage.setObj('myObj', myObj);
}
Get data
function ShowItem() {
var aux = localStorage.getObj('myObj');
console.log(aux);
}
$(document).ready(function () {
Storage.prototype.setObj = function (key, obj) {
return this.setItem(key, JSON.stringify(obj))
}
Storage.prototype.getObj = function (key) {
return JSON.parse(this.getItem(key))
}
var myObj = [];
localStorage.setObj('myObj', myObj);
});
Save item in local storage
function SaveItem() {
var myObj = localStorage.getObj('myObj');
var objNome = $('#txtFstName').val();
var objApelido = $('#txtLastName').val();
var obj = { 'fstName': objNome, 'lastName': objApelido, 'age': 32 };
myObj.push(obj);
localStorage.setObj('myObj', myObj);
}
Get data
function ShowItem() {
var aux = localStorage.getObj('myObj');
console.log(aux);
}
Comments
Post a Comment