- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
JavaScript Prompt Example
The prompt dialog box is very useful when you want to pop-up a text box to get user input. Thus, it enables you to interact with the user. The user needs to fill in the field and then click OK. This dialog box is displayed using a method called prompt()
Following is the code for showing prompt in JavaScript −
Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>JavaScript Prompt() method</h1> <div class="result"></div> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to get the prompt dialogue box</h3> <script> let resultEle = document.querySelector(".result"); document.querySelector(".Btn").addEventListener("click", () => { let res = prompt("Are you sure"); if (res === null) { resultEle.innerHTML = "You pressed cancel"; } else { resultEle.innerHTML = "You entered " + res + " in the prompt box"; } }); </script> </body> </html>
Output
On clicking on ‘CLICK HERE’ button and typing something in the promt box −
On clicking OK on the prompt box −
- Related Articles
- How to show prompt dialog box using JavaScript?
- How can I execute JavaScript at the command prompt?
- Enter values with prompt and evaluate on the basis of conditions in JavaScript?
- JavaScript Numbers example
- Prompt mode in 8085 Microprocessor
- JavaScript console.log() with Example
- JavaScript getPrototypeOf with Example
- JavaScript JSON.stringify() with Example
- JavaScript Math Object example
- JavaScript unescape() with example
- Getting MySQL path in command prompt
- Print structured MySQL SELECT at command prompt
- Creating a prompt dialog box using Tkinter?
- Inheritance in JavaScript with example
- Sorting Prompt values in SAP BO Webi report

Advertisements