I want to create a JavaScript function that will create an HTML form and return the input the user enters into the form. My stupid first attempt was something like this:
function getInput() {
$('#somediv').html( "<input type='button' id='mybutton' name='Click Me'></input>" );
$('#mybutton').click( function() { return "result"; } );
}
result = getInput();
alert( result );
(The actual situation is more complex, the user has to first enter some text and then choose between several different buttons, and the return value depends on the entered text and the chosen button, but I think those are peripheral issues so I left them out). If I run this as written, I'll get alert "undefined" where what I want is alert "result". Essentially I want the function not to return until it's explicitly told to do so. Is there any way to achieve this? If not, what's the simplest workaround that's similar in spirit to the above?
A very similar question was posed here: JavaScript pausing execution of function to wait for user input. I'm afraid I just couldn't understand the responses well enough to adapt them to my particular case. Perhaps someone could advise how to do so, or alternatively starting from scratch would be fine too.