How to add some text value on LocalSorage using HTML5?

Hi experts, it is possible to add some field on the value of LocalStarage using HTML5? heres my codes;

    <script>
        function save()
        {
        var fieldValue = document.getElementById('firstName').value;
        localStorage.setItem('text',fieldValue);
        alert("Successfully saved!");
        }
    </script>

What I need is to add another field for the familyName. It is possoble?
Whing Dela CruzAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Brian TaoSenior Business Solutions ConsultantCommented:
Something like this would work:
 <script> 
        function save()
        {
        var fieldValue;
        fieldValue = document.getElementById('firstName').value;
        localStorage.setItem('firstName',fieldValue);
        fieldValue = document.getElementById('familyName').value;
        localStorage.setItem('familyName',fieldValue);
        alert("Successfully saved!");
        }
    </script>

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Whing Dela CruzAuthor Commented:
Hello Brian Tao, thanks its working. Can I make a table in the LocalStorage? I want to put in a row the First name and the lastname in different columns. It is possble?
Brian TaoSenior Business Solutions ConsultantCommented:
Then you will first need to put them into an object (or an array of objects if you have multiple rows in it), and then stringify (or serialize) it and save it :
 <script> 
    function save()
    {
        var fieldValues = [];  // prepare the array
        var tmpValue = {}; // prepare the object

        tmpValue.firstName = document.getElementById('firstName').value;
        tmpValue.familyName = document.getElementById('familyName').value;
        fieldValues.push(tmpValue);

        // and then add more rows if you want to
        tmpValue.firstName = document.getElementById('firstName2').value;
        tmpValue.familyName = document.getElementById('familyName2').value;
        fieldValues.push(tmpValue);

        // continue adding rows.  if you do have multiple rows, consider using a loop here instead of the way I wrote it.

        localStorage.setItem('nameTable', JSON.stringify(fieldValues));
        alert("Successfully saved!");
    }
</script>

Open in new window

Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Whing Dela CruzAuthor Commented:
Hi Brian Tao, Please tell me if I got the correct... I attached the result so that you will see and I am not so sure if I coded correctly. the result of what Brian Tao coded above...
Whing Dela CruzAuthor Commented:
Hi Brian Tao, sorry but as i observed only the firstName2 and familyName2 has been saved. The first firstName and familyName has not. I just copy and paste your code above...
Brian TaoSenior Business Solutions ConsultantCommented:
I see 2 sets of {firstName, familyName} pairs in your screenshot, so it should be working.  Do you have a sample page so that I can link to it and check?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
HTML

From novice to tech pro — start learning today.