SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    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";
    localStorage.clear();
    }

    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";
    localStorage.removeItem("document.form2['aboutSetupOne']");
    }

    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?

    Thanks

  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by StevenHu View Post
    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.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

    Thanks!

  4. #4
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by StevenHu View Post
    Here is how I am setting this value in localStorage successfully:
    That's odd, for I'm not seeing localStorage.setItem() used anywhere in what you've shown me.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  5. #5
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    setItem doesn't have to be called, data can be written directly.
    Tab-indentation is a crime against humanity.

  6. #6
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  7. #7
    SitePoint Guru
    Join Date
    Sep 2006
    Posts
    731
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StevenHu View Post
    This code does not erase the field called 'aboutSetupOne' in form2:

    function clearData()
    { "use strict";
    localStorage.removeItem("document.form2['aboutSetupOne']");
    }

    This code does not erase the form data:

    function clearData()
    { "use strict";
    localStorage.removeItem("form2"); // also doesn't work: "document.form2"
    }
    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.
    Tab-indentation is a crime against humanity.

  8. #8
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Logic Ali View Post
    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?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  9. #9
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,338
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Perhaps I am using localStorage in a non-standard way. I was going according to the instructions here:
    http://marxsoftware.blogspot.com/201...b-storage.html

    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?

    Thanks

  10. #10
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,678
    Mentioned
    99 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by StevenHu View Post
    Perhaps I am using localStorage in a non-standard way. I was going according to the instructions here:
    http://marxsoftware.blogspot.com/201...b-storage.html

    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?
    Code:
    localStorage.removeItem(key) // to remove an item from local storage.
    localStorage.addItem(key, value) // to add an item to local storage.
    The page that you referred to earlier uses a shortcut to add an item, which is:

    Code:
    localStorage[key] = value;

    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.
    http://marxsoftware.blogspot.co.nz/2...20954907987696

    I don't know how to make it any clearer that that. Use localStorage.removeItem(key) to remove an item from local storage.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •