Tutorials » Javascript tutorials »

Working with confirm and prompt


This article explains usage of the confirm and prompt in Javascript


JavaScript confirm



As mentioned earlier confirm is used for choosing an option from two choices. It will return a Boolean value. Let’s have a look at the example.


<html>

<body>

<script type="text/javascript">

var v=window.confirm("Are you sure you are ok?");

window.alert(v);

</script>

</body>

</html>




Have you observed the fact? Right!

If you are clicked on OK it returns true so value of v is true. If you are clicked on Cancel it returns false so value of v is false, that is displayed dynamically on an alert box based on the click.

Here did you notice the thing that is new var v. Yeah this is how we declare variables in JavaScript.
var v;

And assignment as follows:
v = 10;

We can even combine declaration and assignment in a single statement. That can be
var v = 10;

Note: Remember JavaScript is case sensitive. The variable v is different from variable V.


Variable declaration in JavaScript is optional. That is if we don't declare a variable it takes as variable type by default.

prompt




<html>

<body>

<script type="text/javascript">

var v=window.prompt("Enter the Number","10");

window.alert(v);

</script>

</body>

</html>



Out put here is self explanatory. We get a prompt dialog box that takes input in prompt box.

The given input is captured or stored in variable v. Value of v is displayed in an alert message.

Here Enter the Number in prompt box title and 10 is the default value.

Just try with by giving window. prompt (); with our any parameters. JavaScript prompt doesn’t specify any default values neither it contains title. So in order to beautify our code it’s recommended to use proper and customized title in all of the boxes.




Next Chapter: prompt, confirm ,alert all together
Previous Chapter: More practical Approach
More Chapters: Javascript Tutorials
More Tutorials: Tutorial Index



Top Contributors
Today
    Last 7 Daysmore...

    Awards & Gifts

    Online Members

    More...
     
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India