JavaScript Prompt

The JavaScript prompt box prompts the user to input text. The 'prompt' box is created using JavaScript's built-in prompt() function. When the JavaScript prompt() function is triggered, a small box will pop up and display a message to the user (supplied by you), a text field (for the user's input), an "OK" button, and a "Cancel" button.

Creating/Triggering a JavaScript Prompt

As mentioned, you create a JavaScript prompt box by calling the built-in JavaScript prompt() function. The function accepts two parameters: Your message to the user (for example, "What is your favorite website?"), and the default response (eg, "").

JavaScript Prompt - Basic Example

You can place the JavaScript prompt() function straight into an HTML tag. Like this:

JavaScript Prompt - A More Useful Example

The above example works, but it doesn't really do much. We asked the user for their input but we didn't do anything with their input!

We could make this more useful by using the user's input to determine what we display to them. So, here's another (more useful) example, where we include the JavaScript prompt() function within another function. Here, we make our website more personal by displaying the user's name in our message to them:

Here's an example: