0
                      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...!

2
  • var x = prompt("enter number"); sav.push(x) Commented Jan 2, 2015 at 21:36
  • 2
    sav[] = x is not valid JavaScript syntax. I believe you're looking for sav.push(x). However, from this code, it's difficult to guess what you're actually trying to accomplish. Please clarify your question. Commented Jan 2, 2015 at 21:36

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

1 Comment

No problem Danish Khan! Don't forget to pick an answer! :-)
0

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);

1 Comment

Thanks Kevin But Saving Values in array not Display

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.