I am trying to work with local storage with forms using html5. I am just unable to find a single working demo online. Can anyone find me a good demo and a tutorial that works. My browser is completely supported.
Appreciate all the help. Thanks
localStorage is similar to sessionStorage , except that while localStorage data has no expiration time, sessionStorage data gets cleared when the page session ends — that is, when the page is closed.
Because HTML5 local storage is natively integrated into Web browsers, it is available without third-party browser plug-ins. It is described in the HTML5 specifications. Local storage is mainly used to store and retrieve data in HTML pages from the same domain.
Save the HTML to localStorage # The innerHTML property returns the HTML inside an element as a string, which makes it the perfect way for us to get and store our list. Let's automatically save a users list every time they add an item to it. You use the localStorage. setItem() method to save data to localStorage .
Here's a jsfiddle demo
(copy of the associated js code, uses of localStorage are called out in the comments)
//Note that if you are writing a userscript, it is a good idea // to prefix your keys, to reduce the risk of collisions with other // userscripts or the page itself. var prefix = "localStorageDemo-"; $("#save").click(function () { var key = $("#key").attr('value'); var value = $("#value").attr('value'); localStorage.setItem(prefix + key, value); //******* setItem() //localStorage[prefix+key] = value; also works RewriteFromStorage(); }); function RewriteFromStorage() { $("#data").empty(); for(var i = 0; i < localStorage.length; i++) //******* length { var key = localStorage.key(i); //******* key() if(key.indexOf(prefix) == 0) { var value = localStorage.getItem(key); //******* getItem() //var value = localStorage[key]; also works var shortkey = key.replace(prefix, ""); $("#data").append( $("<div class='kvp'>").html(shortkey + "=" + value) .append($("<input type='button' value='Delete'>") .attr('key', key) .click(function() { //****** removeItem() localStorage.removeItem($(this).attr('key')); RewriteFromStorage(); }) ) ); } } } RewriteFromStorage();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With