Danish Khan
Danish Khan

Reputation: 23

How Get value of JS promptbox then save into Array

                      var sav =new Array();
                       sav[] =prompt("Enter value");
                           while(a!="x")
                             {
                     var a =prompt("Enter value");
                             sav[a]=a;
                              }
                            if(a==x)
                            document.write(sav[a]);

How Get value of JS promptbox then save into Array help...!

Upvotes: 0

Views: 48

Answers (2)

Adam
Adam

Reputation: 2077

Here's a JSFiddle for an example of how to do this with a function so you can reuse the code multiple times.

Here's the code:

var responses = [];

var askAUser = function() {
    var userText = prompt("Enter text:");
    responses.push(userText);
    return responses;
};

// Ask by invoking function
askAUser();

// Shows that the user info has been added to the array.
console.log(responses);

Upvotes: 1

Kevin Bowersox
Kevin Bowersox

Reputation: 94459

Invoke the push() function to add the retrieved value to the array.

sav.push( prompt("Enter value"));

Working Example: http://jsfiddle.net/uz5s2vpL/

If you want to use the value elsewhere within the code, store it within a variable.

  var sav = [];
  var result = prompt("Enter value"));
  sav.push(result);

Upvotes: 0

Related Questions