I have 2 apps working together with localstorage and I was wondering how can I delete all the keys which start with note- and todo- . I know localstorage.clear() clears everything but thats not my aim.
Here is an example of what I have in my localstorage:
Where I want to delete all the todo-* with a button click and all note-* with other button click using jquery.
Thanks alot
HTML5 local storage is a component of the Web storage application programming interface. It is a method by which Web pages locally store named key/value pairs inside a client's Web browser.
HTML web storage provides two objects for storing data on the client: window. localStorage - stores data with no expiration date.
Consequently, HTML5 has greatly simplified the process of creating web applications. Thanks to HTML5, web pages can now store data locally on the user's browser, which eliminates the need for HTTP cookies. As a result, content can be delivered faster and more securely.
The local storage is a type of HTML5 offline storage that allows user string data to be saved synchronously in their browser. Information is kept in name and value pairs and not available between different browsers on the same device. Local storage can be used as an alternative to cookies.
Object.keys(localStorage) .forEach(function(key){ if (/^todo-|^note-/.test(key)) { localStorage.removeItem(key); } });
I used a similar method to @Ghostoy , but I wanted to feed in a parameter, since I call this from several places in my code. I wasn't able to use my parameter name in a regular expression, so I just used substring instead.
function ClearSomeLocalStorage(startsWith) { var myLength = startsWith.length; Object.keys(localStorage) .forEach(function(key){ if (key.substring(0,myLength) == startsWith) { localStorage.removeItem(key); } }); }
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