0

I'm currently writing functions for alternatives to built-in functions such as prompt. I want to be able to ask a question and have the user type into an input box, and return the value when the user presses a submit button. I can't seem to figure out how I can pause the function until the submit button is pressed, and then return the value. I want to be able to call multiple of these functions to have the user be prompted sequentially. Is this even possible? How can I go about this in a simple and reproducible way, so I can implement this into other functions? Here's an example of what I want to do

input("What is your name?");

The input function should pause and wait for a submit button to be pressed and return the value of the input field. Plain and simple.

Nerd
  • 11
  • 1
  • 1
  • Use a callback that gets triggered when your value is entered - `input("What is your name?", function(answer) {// use \`answer\`})`. You can make this [promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) based to avoid nested callbacks – Nick Parsons May 23 '21 at 01:26
  • Are you using a form with input fields? If yes, you can attach an onsubmit or onclick events listener to the form or to the submit button, and process the value of the input fields when someone clicked on the button or submit the form. – iismaell May 23 '21 at 01:49

1 Answers1

0

No, it is impossible, You can't make input function synchronous pause.

Is it possible to simulate a blocked dialog like 'window.confirm()'?

But, you can combine promise and event to do asynchronous pause. make a promise await to a lock, and only submit can unlock.

yang zhou
  • 148
  • 6