Home 〉 Javascript Tutorials 〉 JavaScript prompt() Tutorial with Examples | Get User Input in JS
JavaScript prompt() Tutorial with Examples | Get User Input in JS !
Ever wondered how to capture user input directly through your web page? The JavaScript prompt() method allows you to display a dialog box that prompts the user for input. This tutorial covers the syntax of prompt(), various methods to implement it, and provides detailed examples for each. By the end, you'll be equipped to use prompt() effectively in your JavaScript projects. Dive into the full tutorial below to enhance your web applications with user interactivity.
The prompt() method in JavaScript displays a dialog box that prompts the user for input. It returns the input value if the user clicks "OK", or null if the user clicks "Cancel". This method is useful for capturing user input without the need for HTML forms.
The basic syntax of the prompt() method is:
html syntax ✍
prompt(text, defaultText);
Parameters:
The method returns the input value as a string if the user clicks "OK", or null if the user clicks "Cancel".
Example 📄
<!DOCTYPE html>
<html>
<body>
<script>
let userName = prompt("Please enter your name:", "John Doe");
if (userName !== null) {
document.write("Hello, " + userName + "!");
} else {
document.write("User cancelled the prompt.");
}
</script>
</body>
</html>
<!DOCTYPE html>
<html>
<body>
<button onclick="showPrompt()">Show Ptompt Button</button>
<script>
function showPrompt() {
let userName = prompt("Please enter your name:", "John Doe");
if (userName !== null) {
alert("Hello, " + userName + "!");
} else {
alert("User cancelled the prompt.");
}
}
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
In this example, a prompt dialog asks the user to enter their name. If the user provides input and clicks "OK", a greeting message is displayed. If the user clicks "Cancel", a cancellation message is shown.
Also, check our complete tutorial on JavaScript alert() to learn about displaying alert messages.
<!DOCTYPE html>
<html>
<body>
<button onclick="prompt('Enter your name:')">Click Me</button>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
The prompt() function is directly called within the onclick attribute of the button. When clicked, it displays the prompt dialog.
<!DOCTYPE html>
<html>
<body>
<button onclick="getUserInput()">Click Me</button>
<script>
function getUserInput() {
let input = prompt("Enter your favorite color:");
alert("Your favorite color is " + input);
}
</script>
</body>
</html>
output 📌
You can Try the above code by changing the values in our user-friendly code editor by clicking the "Try It" button and see the output of the same.
Here, the prompt() function is encapsulated within a JavaScript function defined in the internal script. The button triggers this function on click.
Example 📄
function askAge() {
let age = prompt("Please enter your age:");
if (age !== null) {
alert("You are " + age + " years old.");
}
}
Example 📄
<script src="external.js"></script>
<button onclick="askAge()">Click Me</button>
In this method, the JavaScript code is written in an external file and included using the src attribute of the <script> tag. This approach promotes code reusability and cleaner HTML.
The JavaScript prompt() method is a straightforward way to capture user input directly through the browser. Whether used inline, internally, or externally, it enhances interactivity in web applications. By understanding its syntax and implementation methods, you can effectively incorporate prompt() into your JavaScript projects.
The prompt() method is used to display a dialog box that prompts the user for input. It returns the input value if the user clicks "OK", or null if the user clicks "Cancel".
Yes, prompt() is supported in all major browsers. However, its appearance and behavior might vary slightly between browsers.
Yes, the input returned by prompt() is always a string. If you need a different data type, you'll need to convert it accordingly.
No, the default prompt dialog's appearance is controlled by the browser and cannot be customized. For custom dialogs, consider using HTML modals.