arrays - How to store multiple items in local storage using an input field -
im working on small database vinyl collection. ik make list of vinyl records have entering each item in input menu wich stores data in local storage , displays them on same html page.
the problem is: can store maximum of 1 item in local storage. when enter second item first item lost. can recall last stored item in local store.
how can create list of multiple item using local storage? i've tryed lookingit can't make work.
here's code:
$(document).ready(function(){ $("#content").append("<tr><td>" + localstorage.album + "</td>" + "<td>" + localstorage.artist + "</td>" + "<td>" + localstorage.year + "</td>" + "<td>" + localstorage.state + "</td></tr>"); $("#save").click(function() { var album = $('#album').val(); var artiest = $('#artiest').val(); var jaar = $('#jaar').val(); var staat = $('#staat :selected').text(); localstorage.album = album; localstorage.artist = artiest; localstorage.year = jaar; localstorage.state = staat; $("#inhoud").append("<tr><td>" + localstorage.album + "</td>" + "<td>" localstorage.artist + "</td>" + "<td>" + localstorage.year + "</td>" + "<td>" localstorage.state + "</td></tr>"); }); });
i'm not quite sure localstorage option that.
what want array of objects (either regular array items sequential, or associative array have key access items). localstorage not support arrays, strings, need serialise (stringify) array before storing localstorage, , deserialise when stuff back.
basically along lines of:
var albums = localstorage.albums ? json.parse(localstorage.albums) : []; albums.push({"album":album, "artist":artiest}); localstorage.albums = json.stringify(albums);
but means lot of parsing , stringifying every change...
Comments
Post a Comment