How do I delete just part of localStorage data?

I know I can delete all the data from local storage on a web page’s input fields with:

function clearData()
{ “use strict”;

But how do I remove data from a specific form or a specific field? This is a specific field in a form:

document.form2[“aboutSetupOne”].value = “”;

This code does not erase the field called ‘aboutSetupOne’ in form2:

function clearData()
{ “use strict”;

This code does not erase the form data:

function clearData()
{ “use strict”;
localStorage.removeItem(“form2”); // also doesn’t work: “document.form2”

Do you know the right syntax to use to selectively erase one forms’ data from local storage?


You would use the same name that was used to set the item in to local storage.

Here is how I am setting this value in localStorage successfully:

        function persistData()
            { "use strict";
                    var aboutSetupOne = document.form2["aboutSetupOne"].value;
                    var storageIndex = "local.storage2.aboutSetupOne";
                    localStorage[storageIndex] = aboutSetupOne;

So I am using the same names.


That’s odd, for I’m not seeing localStorage.setItem() used anywhere in what you’ve shown me.

setItem doesn’t have to be called, data can be written directly.

The name you are setting it with is “local.storage2.aboutSetupOne”

Have you tried using that same name to remove it with?

Either I have completely misunderstood your question or you misunderstand the nature of localStorage.
You seem to think that localStorage is a direct mechanism for making form data permanent. Maybe you should research the subject further.

I don’t think that localStorage has anything to do with forms. You can refer to the pages 251-255 of our HTML5 & CSS3 for the real world for details on localStorage.

Am I misunderstanding something?

Perhaps I am using localStorage in a non-standard way. I was going according to the instructions here:

The user fills in a field, and presses Submit, and the data is saved to local storage. clearData() is used to clear all the user’s entries. So my question is how to clear only the one entry of one field?


localStorage.removeItem([I][COLOR="#696969"]key[/COLOR][/I]) // to remove an item from local storage.
localStorage.addItem([I][COLOR="#696969"]key[/COLOR][/I], [I][COLOR="#696969"]value[/COLOR][/I]) // to add an item to local storage.

The page that you referred to earlier uses a shortcut to add an item, which is:

localStorage[[I][COLOR="#696969"]key[/COLOR][/I]] = [I][COLOR="#696969"]value[/COLOR][/I];

There is also a reply from the author of that blog post that also says that you should use localStorage.removeItem(key) to remove an item from the local storage.

I don’t know how to make it any clearer that that. Use localStorage.removeItem(key) to remove an item from local storage.