Quick Reach
The prompt dialog type
The prompt dialog is the way to ask a user for some input value in the form of a dialog box. User is given Ok and Cancel options to proceed after entering text. If Ok is pressed it will return entered value. If Cancel is pressed, the null is returned.
See a prompt dialog example
Syntax of using prompt box
This is how you can display javascript prompt dialog box:
prompt(“Message to be shown”, “default text in JS prompt box”);
You may enter default text inside JS prompt box.
Or use “window” prefix as follows:
window.prompt(“Message to be shown”, “default text in JS prompt box”);
Javascript dialog example with default value
Following is an example of javascript prompt dialog box. If a user enters some value this will be shown in a simple alert. See example by clicking the link below:
Experience this example online
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<button onclick=“showalert()”>Show prompt JS</button>
<script type=“text/javascript”>
function showalert(){
var getval = prompt(“Enter text here, we will display in alert!”, “Default text”);
if (getval != null) {
alert(“Entered text is: “ + getval);
}
else {
alert (“Cancel clicked!”);
}
}
</script>
</body>
</html>
|
Other dialog boxes
There are other javascript dialog boxes as well with simple OK or OK and Cancel options without prompt. Following are links to its chapters with examples.
Also see Javascript alert | Javascript confirm
Leave A Comment?