prompt function
The prompt function displays
a window for data entry. The first
parameter it takes is the message
that will be shown to the user, and
the second parameter is the default
text in the input field. The second
parameter is optional.
In the window that will appear there
will be a text field, and two buttons
- OK, CANCEL. When you click on OK
- the string entered by the user is
returned, and when you click on
CANCEL - null.
Syntax
prompt(message, [default text]);
Example
Upon pressing a button, we will ask for
the user's name, and then display this
name using the
alert
function:
<button id="button">click me</button>
let button = document.querySelector('#button');
button.addEventListener('click', function(event) {
let res = prompt('What is Your name?');
alert(res);
});
:
Example
Let's make it so that the username will already be entered by default, but the user will be able to change it:
<button id="button">click me</button>
let button = document.querySelector('#button');
button.addEventListener('click', function(event) {
let res = prompt('What is Your name?', 'John');
alert(res);
});
: