User Input - Confirm & Prompt
EasyProgramming.net tutorial - learn how to ask for user input using prompt and confirm
HTML
<!-- Easy JavaScript #8 - Prompt and Confirm -->
<p>
Data input is essential in any web interface. It makes web applications interactive, it takes them from Web 1.0 to Web 2.0.
</p>
<p>
<code>confirm()</code> is another method of the window object which allows you to ask the user to confirm their choice, either okay (true) or cancel/close (false). You can then use the boolean input to trigger functions and other actions within your script.
</p>
<p>
<code>prompt()</code> is a method of the window object which allows you to prompt the user for an input. You can store the data into a variable, manipulate it, and output it any way and anywhere you'd like.
</p>
<div id="answer"></div>
<div id="myInput"></div>
<p>
<a href="http://www.easyprogramming.net">EasyProgramming.net</a>
</p>
CSS
#myInput{
font-size: 30px;
color: red;
font-weight: bold;
}
#answer{
font-size: 30px;
color: blue;
font-weight: bold;
}
JavaScript
// Store the two div IDs into variables
var answer = document.getElementById("answer");
var myInput = document.getElementById("myInput");
/* syntax of confirm:
confirm("enter text here");
*/
var confirmBox = confirm("Do you want to continue?");
answer.innerHTML = confirmBox;
/* syntax of prompt:
prompt("enter question");
prompt("enter question", "enter default input")
*/
var name = prompt("Enter your name","My Name");
myInput.innerText = "Hello " + name + "!";