Brendan Lesniak
Brendan Lesniak

Reputation: 2321

jQuery and Local Storage unusual behavior

I am trying to retrieve some items from local storage, and if they are there, reveal some content on the page. Here is what I am trying to do:

function loadData()
{       
    var transData = storage.get('TransInfo');

    if(transData != undefined)
    {
        $('#UserName').val(transData[0]);

        if(transData[1] == 'Blue')
        {
            $('#radio-choice-1').attr('checked', true);
        }
        else
        {
            $('#radio-choice-2').attr('checked', true);
        }

        $('#TransNum').val(transData[2]);

        if(transData[3] == 'A')
        {
            $('#radio-choice-1-board').attr('checked', true);
        }
        else
        {
            $('#radio-choice-2-board').attr('checked', true);
        }

        unHideAll();
        return true;
    }
};

And when the data is finally loaded, I want to call unHideAll():

//THIS FUNCTION FAILS WHEN CALLED!!
function unHideAll()
{
    /*
    $('#radio-choice-1').checkboxradio('enable');
    $('#radio-choice-2').checkboxradio('enable');
    $('#radio-choice-1-board').checkboxradio('enable');
    $('#radio-choice-2-board').checkboxradio('enable');
    $('#TransNum').textinput('enable'); 
    $('#UserContinue').remove();
    $('#nextButton').show();
    */
};

The problem I am getting is unHideAll() never gets anywhere. If I put an alert() at the top, it will show, but if I put an alert() at the bottom, it never gets there. Any idea why?

Here is how I am calling the methods:

$(document).ready(function()
{   
    clearStorage(); 
    loadData();
    unHide();           
    collectData();
});

Any idea why I can't get unHideAll() to do anything? It works in the traditional unHide() method, which I use when there is no local storage yet, but if local storage is present it tends to fail quickly.

Any ideas?

EDIT:

Here is my local storage code:

window.storage = {
    store:localStorage,
    get: function( key ) 
    {
        try
        {
            return JSON.parse(this.store[key]);
        } 
        catch(e) {};

        return undefined;
    },
    set: function( key, value) 
    {
        try
        {
            this.store[key] = JSON.stringify(value);
        } 
        catch(e) {};
    }
};

EDIT 2:

And here is some code that works, which is why it's puzzling why it won't work in my new method.

//THIS CODE WORKS FLAWLESSLY TO REVEAL ITEMS!!
$('#UserContinue').click(function() 
{
    if($('#UserName').val() == '') 
    {
        alert('Please Enter a User Name First!');
        return false;
    }
    else 
    {
        User_Name = $('#UserName').val();
        $('#radio-choice-1').checkboxradio('enable');
        $('#radio-choice-2').checkboxradio('enable');
        $('#radio-choice-1-board').checkboxradio('enable');
        $('#radio-choice-2-board').checkboxradio('enable');
        $('#TransNum').textinput('enable'); 
        $('#UserContinue').remove();
        $('#nextButton').show();

        confirm('Welcome ' + User_Name +'!');

        return true;
    }

    return true;
});

Which is really weird...

Upvotes: 1

Views: 452

Answers (2)

thecodeparadox
thecodeparadox

Reputation: 87083

You can simply try this:

window.storage = {
    store:localStorage,
    get: function( key ) 
    {  
      var ret = this.store.getItem('key');
      if(ret) {
           return JSON.parse(ret);
       }
        return undefined;
    },
    set: function( key, value) 
    { 
        this.store.setItem(key, JSON.stringify(value));
    }
};

NOTE:

  1. In your get function I don't see anything that read the localStorage
  2. set function don't set the data to localStorage

Upvotes: 0

Jocelyn LECOMTE
Jocelyn LECOMTE

Reputation: 895

The code of unHideAll() is completely wrong. You don't have methods like checkboxradio() or textinput(). This is why, when the function is called, an error occurs and produces the behavior you explain.
To enable / disable form elements with jQuery, read the FAQ: http://docs.jquery.com/Frequently_Asked_Questions#How_do_I_disable.2Fenable_a_form_element.3F

Upvotes: 2

Related Questions