In JavaScript Prompt box is used to get the single input value from the user that can be further used as per requirements. Prompt box displays OK and CANCEL button along with a string label that you want to ask to the visitor and an input text box to get the input from user. If the user enters a string into the text box and clicks OK button then prompt box returns the input entered by the user, and if the user clicks the CANCEL button then prompt box returns null.
Syntax for JavaScript Prompt Box
prompt([message], [default string]);
Example of JavaScript Prompt Box
function promptMessage() {
var inputVal = prompt('Please enter your screen name:', 'Guest');
document.write('Your screen name is:' + inputVal);
}
Above example will display the string label ‘Please enter your screen name:’ and ‘Guest’ in the input field. If user will click on OK button directly then it will display the string ‘Your screen name is: Guest’, if user will clicks the CANCEL button then it will write the string ‘Your screen name is: null‘.
Continue to next tutorial: JavaScript Popup Using Window.Open to learn how to open popup window using JavaScript.