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.

Recent JavaScript Tutorials

Javascript FSO ShortPath property

Javascript FileSystemObject (FSO) provides a ShortPath property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Path Property

Javascript FileSystemObject (FSO) provides the Path property that can be accessed through the file object or folder object returned by the GetFile or GelFolder method

Javascript FSO Type property

Javascript FileSystemObject (FSO) provides a Type property that can be accessed through file object or folder object returned by the GetFile or GetFolder method

Javascript FSO ShortName property

Javascript FileSystemObject (FSO) provides a ShortName property that can be accessed using the folder object or file object returned by the GetFolder or GetFile method.

Javascript FSO Name Property

Javascript FileSystemObject (FSO) provides a Name property that can be accessed using file object or folder object.