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

  • Home
  • SEARCH
  • 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 336159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T10:12:58+00:00 2026-05-12T10:12:58+00:00

Is there a way to create a dynamic array of strings on Javascript? What

  • 0

Is there a way to create a dynamic array of strings on Javascript?
What I mean is, on a page the user can enter one number or thirty numbers, then he/she presses the OK button and the next page shows the array in the same order as it was entered, one element at a time.

Code is appreciated.

  • 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-12T10:12:59+00:00Added an answer on May 12, 2026 at 10:12 am

    What I mean is, on a page the user can enter one number or thirty numbers, then he/she presses the OK button and the next page shows the array in the same order as it was entered, one element at a time.

    Ok, so you need some user input first? There’s a couple of methods of how to do that.

    1. First is the prompt() function which displays a popup asking the user for some input.
      • Pros: easy. Cons: ugly, can’t go back to edit easily.
    2. Second is using html <input type="text"> fields.
      • Pros: can be styled, user can easily review and edit. Cons: a bit more coding needed.

    For the prompt method, collecting your strings is a doddle:

    var input = []; // initialise an empty array
    var temp = '';
    do {
        temp = prompt("Enter a number. Press cancel or leave empty to finish.");
        if (temp === "" || temp === null) {
            break;
        } else {
            input.push(temp);  // the array will dynamically grow
        }
    } while (1);
    

    (Yeah it’s not the prettiest loop, but it’s late and I’m tired….)

    The other method requires a bit more effort.

    1. Put a single input field on the page.
    2. Add an onfocus handler to it.
      1. Check if there is another input element after this one, and if there is, check if it’s empty.
        • If there is, don’t do anything.
        • Otherwise, create a new input, put it after this one and apply the same handler to the new input.
    3. When the user clicks OK, loop through all the <input>s on the page and store them into an array.

    eg:

    // if you put your dynamic text fields in a container it'll be easier to get them
    var inputs = document.getElementById('inputArea').getElementsByTagName('input');
    var input = [];
    for (var i = 0, l = inputs.length; i < l; ++i) {
        if (inputs[i].value.length) {
            input.push(inputs[i].value);
        }
    }
    

    After that, regardless of your method of collecting the input, you can print the numbers back on screen in a number of ways. A simple way would be like this:

    var div = document.createElement('div');
    for (var i = 0, l = input.length; i < l; ++i) {
        div.innerHTML += input[i] + "<br />";
    }
    document.body.appendChild(div);
    

    I’ve put this together so you can see it work at jsbin
    Prompt method: http://jsbin.com/amefu
    Inputs method: http://jsbin.com/iyoge

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 199k
  • Answers 199k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is one of those 'how long is a piece… May 12, 2026 at 7:47 pm
  • Editorial Team
    Editorial Team added an answer I'm afraid you're on your own there. Constructors aren't inherited… May 12, 2026 at 7:47 pm
  • Editorial Team
    Editorial Team added an answer I just realized that I can find this out in… May 12, 2026 at 7:47 pm

Related Questions

I have a class that contains a dynamically allocated array, say class A {
I have a function that gets an array of DOM elements (based on tags)
I am trying to create a class as such: class CLASS { public: //stuff
I am creating a web application that acts as a priority list, allowing a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.