SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    I want a blank field, not "undefined"

    When I clear the memory of HTML5 WebStorage (or local storage), then open the page again to look at the fields, they are filled with "undefined." I'd like them to be blank. How do I do that?

    Here is the code used for loading the memory back into the fields at the click of a button. Instead of a blank field for each textarea, "undefined" is in each field:

    Code:
                function loadData()
                {
                    //front suspension
                        document.form1["s1frontTowerMtgShock3"].value = localStorage["practicelog.html.s1frontTowerMtgShock3"];
                        document.form1["s1frontTowerMtgShock2"].value = localStorage["practicelog.html.s1frontTowerMtgShock2"];
                        document.form1["s1frontTowerMtgShock1"].value = localStorage["practicelog.html.s1frontTowerMtgShock1"];
                        document.form1["s1frontTowerShockMtgOther"].value = localStorage["practicelog.html.s1frontTowerShockMtgOther"];
                        document.form1["note3"].value = localStorage["note3"];
    Here is the code for deleting the memory:

    Code:
                function clearData()
                {
                    //if (html5StorageSupported())
                    //{
                        // Clear data from local storage
                        localStorage.clear();
                        document.form1["s1frontTowerMtgShock3"].value = "";
                        document.form1["s1frontTowerMtgShock2"].value = "";
                        document.form1["s1frontTowerMtgShock1"].value = "";
                        document.form1["s1frontTowerShockMtgOther"].value = "";
                        document.form1["Note3"].value = "";
    
                        document.form1.numStored.value = determineNumberStoredElements();
                   // }
                }
    Somehow I need to check for a null field and set it to "" instead of the default "undefined". How do I do that?

    Thanks!

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    Try something like this in loadData() for each field:
    Code:
    document.form1["s1frontTowerMtgShock3"].value = localStorage["practicelog.html.s1frontTowerMtgShock3"].length>0 ? localStorage["practicelog.html.s1frontTowerMtgShock3"] : '';
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Wizard
    Join Date
    Feb 2007
    Location
    Southern California
    Posts
    1,340
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    No, that did not work. Why did you use .length>0?

  4. #4
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    265
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi,
    I used .length>0 to test the value to have at least one character. But instead of this property, try to test if its undefined, != null or != undefined .
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners


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
  •