Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7622707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:31:11+00:00 2026-05-31T04:31:11+00:00

I am trying to use localStorage to save an array of strings. I have

  • 0

I am trying to use localStorage to save an array of strings. I have a function addToFav(string) which is meant to load the string value into an array which holds program names, or pNames in my case. I’m trying to follow this outline: How do I store an array in localStorage?

localStorage.arrayNum = 0;
var pNames=[];


function addToFav(string)
{
    pNames[localStorage.arrayNum] = string;

    if (localStorage.arrayNum)
    {
        localStorage.arrayNum=Number(localStorage.arrayNum)+1;
    }
    else
    {
        localStorage.arrayNum=1;
    }

    localStorage['pNames']=JSON.stringify(pNames);
    var storedNames=JSON.parse(localStorage['pNames']);

    document.getElementById("testField").innerHTML=storedNames;

}

When I click the button that invokes addToFav(string), the string that I passed into the function is displayed in the testfield div, which is OK. However, launching the function again should add another element to the array but it doesn’t, it just repeatedly adds to pNames[0] which is because I think that the pNames and arrayNum are getting redeclared everytime. With this, does anyone know how to prevent this problem? I would like to create an array that adds favourite items when the user invokes my function.

Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T04:31:12+00:00Added an answer on May 31, 2026 at 4:31 am

    It’s because you are using pNames as your key each time.

    localStorage['pNames']=JSON.stringify(pNames);
    

    You should do something like:

    localStorage['pNames'+someVal]=JSON.stringify(pNames);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying use the edit_post_link() function to contain an image. All of
I have been trying to use localstorage to store Jquery-min but I am unable
I have a regex that I'm trying use to validate against strings. Trying to
I am trying to use local storage to be able to save/load multiple sets
Trying to use an excpetion class which could provide location reference for XML parsing,
Trying to use this method (gist of which is use self.method_name in the FunnyHelper
I've been trying to use localStorage with a textarea in my webpage (to no
I trying to use Client Side Storage available in HTML5 (localStorage) for Iphone Application
I have a variable localStorage.lastUpdate which stores a timestamp such as 1332237161 . I
i'm trying use webview to load a image from sdcard i use this path

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.