3

I have looked for this, but haven't found anything similar. In Javascript, I want to know if there is a way to get user input, from say a text area, in the middle of a loop, such as with the input() function from python. Here is an example:

 def thing():
    color = input("Pick Red, Yellow or Blue")
    finished = False
    previous = []
    while finished == False:
        if previous == []:
            guess = "red"
            response = input("Is your color red?")
            previous.append((guess, response))
        elif len(previous) == 1:
            response = input("is your color blue?")
            if response == "yes":
                finished = True

This example is not exactly what I am trying to do but basically, I want a way, for a user to be asked things, and for them to put an answer in a text area(HTML), and push a button, which continues a Javascript while loop with the newly received input, and formulates some other question, and so on, until some condition is reached to stop the loop.

Is this possible? Does something in JS mimic this?

3
  • 3
    You can use prompt('Pick red, Yellow or Blue') in order to make browser show dialog which will ask something and wait for input. However, if you want to do this in HTML - you will need to use callbacks. Commented Nov 19, 2015 at 3:54
  • Whilst prompt does do this, it can be pretty UX breaking. Consider making your code event-driven and using an <input> Commented Nov 19, 2015 at 3:56
  • Paul S: The issue with it being event driven is that I also need a way to store the previous responses the user puts in, so I thought the best way to do that is in the middle of a while loop. Is there another way to do event driven while storing responses in an immediately accessible way? Commented Nov 19, 2015 at 4:05

1 Answer 1

5

Good old readline();

https://developer.mozilla.org/En/SpiderMonkey/Introduction_to_the_JavaScript_shell#readline()

If you want to get value from text area then use :

<script>
function myFunction() {
    var x = document.getElementById("myTextarea").value;
}
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

Hmmm, yes that would essentially work but problem is I need it to store previous responses as a memoization, such as by appending them to an array while the function is running. I think I will have to user callbacks for this.

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.