- JavaScript Basics
- JavaScript Tutorial
- JavaScript: where to write
- JavaScript: how to display
- JavaScript: keywords
- JavaScript: comments
- JavaScript: variables
- JavaScript: operators
- JavaScript: data types
- JavaScript Conditional Statements
- JavaScript: if-else
- JavaScript: switch
- JavaScript: for loop
- JavaScript: while loop
- JavaScript: do-while loop
- JavaScript: break and continue
- JavaScript Popup Boxes
- JavaScript: alert box
- JavaScript: confirm box
- JavaScript: prompt box
- JavaScript Popular Topics
- JavaScript: functions
- JavaScript: innerHTML
- JavaScript: getElementById()
- JavaScript: getElementsByClassName()
- JavaScript: getElementsByName()
- JavaScript: getElementsByTagName()
- JavaScript: querySelector()
- JavaScript: querySelectorAll()
- JavaScript: document.write()
- JavaScript: console.log()
- JavaScript: boolean
- JavaScript: events
- JavaScript: Math object
- JavaScript: Math.random()
- JavaScript: Number()
- JavaScript: parseInt()
- JavaScript: parseFloat()
- JavaScript Arrays
- JavaScript: array
- JavaScript: find length of array
- JavaScript: add element at beginning
- JavaScript: add element at end
- JavaScript: remove first element
- JavaScript: remove last element
- JavaScript: get first index
- JavaScript: get last index
- JavaScript: reverse an array
- JavaScript: sort an array
- JavaScript: concatenate arrays
- JavaScript: join()
- JavaScript: toString()
- JavaScript: from()
- JavaScript: check if value exists
- JavaScript: check if array
- JavaScript: slice an array
- JavaScript: splice()
- JavaScript: find()
- JavaScript: findIndex()
- JavaScript: entries()
- JavaScript: every()
- JavaScript: fill()
- JavaScript: filter()
- JavaScript: forEach()
- JavaScript: map()
- JavaScript Strings
- JavaScript: string
- JavaScript: length of string
- JavaScript: convert to lowercase
- JavaScript: convert to uppercase
- JavaScript: string concatenation
- JavaScript: search()
- JavaScript: indexOf()
- JavaScript: search() vs. indexOf()
- JavaScript: match()
- JavaScript: match() vs. search()
- JavaScript: replace()
- JavaScript: toString()
- JavaScript: String()
- JavaScript: includes()
- JavaScript: substr()
- JavaScript: slice string
- JavaScript: charAt()
- JavaScript: repeat()
- JavaScript: split()
- JavaScript: charCodeAt()
- JavaScript: fromCharCode()
- JavaScript: startsWith()
- JavaScript: endsWith()
- JavaScript: trim()
- JavaScript: lastIndexOf()
- JavaScript Date and Time
- JavaScript: date and time
- JavaScript: Date()
- JavaScript: getFullYear()
- JavaScript: getMonth()
- JavaScript: getDate()
- JavaScript: getDay()
- JavaScript: getHours()
- JavaScript: getMinutes()
- JavaScript: getSeconds()
- JavaScript: getMilliseconds()
- JavaScript: getTime()
- JavaScript: getUTCFullYear()
- JavaScript: getUTCMonth()
- JavaScript: getUTCDate()
- JavaScript: getUTCDay()
- JavaScript: getUTCHours()
- JavaScript: getUTCMinutes()
- JavaScript: getUTCSeconds()
- JavaScript: getUTCMilliseconds()
- JavaScript: toDateString()
- JavaScript: toLocaleDateString()
- JavaScript: toLocaleTimeString()
- JavaScript: toLocaleString()
- JavaScript: toUTCString()
- JavaScript: getTimezoneOffset()
- JavaScript: toISOString()
- JavaScript Regular Expression
- JavaScript: regular expression
- JavaScript: RegEx . (dot)
- JavaScript: RegEx \w and \W
- JavaScript: RegEx \d and \D
- JavaScript: RegEx \s and \S
- JavaScript: RegEx \b and \B
- JavaScript: RegEx \0
- JavaScript: RegEx \n
- JavaScript: RegEx \xxx
- JavaScript: RegEx \xdd
- JavaScript: RegEx quantifiers
- JavaScript: RegEx test()
- JavaScript: RegEx lastIndex
- JavaScript: RegEx source
- JavaScript Programs
- JavaScript Programs
JavaScript prompt()/window.prompt(): Prompt Dialog Box
JavaScript is a potent tool for creating dynamic user experiences and adding interactivity to web pages. The prompt() function is one of the built-in JavaScript functions that accepts user input. This function gives programmers the ability to ask the user for a value or response, creating a myriad of engaging and interactive web applications. So let's start with a brief definition.
The JavaScript prompt() method is used to create a prompt dialog or popup box that is obviously used to get input from the user, either before entering into a page or to use the input for the current page.
The prompt() function syntax in JavaScript
The syntax of the prompt() method in JavaScript is:
prompt("direction", "default");
The direction parameter refers to a text or message that is used to display some direction to the user, and the default parameter is used to put a prior value in the input box. For example:
<!DOCTYPE html> <html> <body> <p id="output"></p> <button onclick="myF()">Click to Enter the Data</button> <script> function myF() { let txt; let x = prompt("Enter Your Favourite Language: ", "JavaScript"); if(x) { txt = "Wow, " + x + " is a Great Language!"; } document.getElementById("output").innerHTML = txt; } </script> </body> </html>
Note: The prompt() function is the same as window.prompt(). It is because window is a global object. And in JavaScript, a method by default belongs to the window object.
The following code is another example demonstrating the "prompt()" function in JavaScript:
<!DOCTYPE html> <html> <body> <p id="msg"></p> <button onclick="myFunction()">Click to Enter Name</button> <script> function myFunction() { let txt; let x = prompt("Enter Your Name: "); if(x==null || x=="") { txt = "You've not entered you name!"; } else { txt = "Welcome " + x; } document.getElementById("msg").innerHTML = txt; } </script> </body> </html>
Note: The getElementById() function returns an HTML element using its ID value.
Note: The innerHTML sets or returns content to or from specified HTML element.
Advantages of the prompt() function in JavaScript
- In many situations, the user can be prompted for input using the straightforward function prompt().
- Text, numbers, and dates are just a few of the input types that can be gathered using the prompt() function.
- prompt() gives the user immediate feedback so they can see the outcome of their input.
- By allowing users to enter data directly into the application rather than navigating to a different form, prompt() can streamline the user interface.
Disadvantages of the prompt() function in JavaScript
- Similar to confirm(), the use of prompt() can break the user's flow and reduce the efficiency of their experience.
- For applications that need a particular look and feel, the prompt dialog box's uncustomizable appearance and behavior can be a drawback.
- When prompt() is used excessively, users may become irritated and lose faith in the application. It's crucial to only employ prompt() when absolutely necessary.
- Given that prompt() depends on user input, mistakes could occur if the user enters inaccurate or invalid data.
« Previous Tutorial Next Tutorial »