Page 213 - Web Applications (803) Class 12
P. 213
Output of the preceding program is as follows:
Example 2—Confirm box
In this example the function display( ) is called and executed when the button is clicked. This calls the confirm
function which prompts the user to press a button. Depending on the button pressed, the respective text is
displayed in the body of the webpage. The getElementById() method returns the element whose id matches.
A specific value is returned otherwise in case the element does not exist, then null is returned. In this example
<p> tag is returned which allows text to be written in the body of the webpage.
<html>
<script>
function display() {
var txt_str;
if (confirm("Press a button!")) {
txt_str = "You pressed OK! Have a great day";
} else {
txt_str = "You pressed Cancel! Goodbye!";
}
document.getElementById("code").innerHTML = txt_str;
}
</script>
<body>
<h2>Using Confirm Box in JavaScript</h2>
<p id="code">
<input type="button" onclick="display()" value="Click Here">
</p>
</body> </html>
Output of the preceding program is as follows:
Web Scripting—JavaScript 211

