Easy tech stuff!

Confirm Box (‘OK’,'CANCEL’ button Message Box)

Posted: February 12th, 2008 | Author: Nischal Shetty | Filed under: JavaScript | No Comments »

Here’s how you display a confirm box using JavaScript.

JavaScript has a function named ‘confirm()‘ which displays the confirm box.

var confirmResult = confirm(“Clicking OK will return true whereas clicking Cancel will return false”);

When you call a JavaScript function containing the above code, a confirm box with two options namely ‘OK’ and ‘CANCEL’ will be displayed. We make use of a variable ‘confirmResult’ to hold the value returned by the ‘confirm()’ function when the user presses ‘OK’ or ‘Cancel’.

A simple example below will make everything clear:

function testingConfirmBox()

{

var confirmBoxReturnValue = confirm(“Click any of the two buttons below and I’ll display an alert telling you the name of the button you clicked :) ”);

if(confirmBoxReturnValue == true)

{

alert(“You clicked ‘OK’ “);

}

else

{

alert(“You clicked ‘Cancel’ “);

}

}

From the poll results of this post I found that this post did not really help a few people. If you did not get what you expected from this post then please leave a comment with your query. May be I would be able to help :)

Note: As far as I know, there is no way of changing the ‘OK’ ‘Cancel’ text to something like ‘Yes’ and ‘No’ unless you hack the browsers source code.

http://www.techtamasha.com/wp-content/plugins/sociofluid/images/digg_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/reddit_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/stumbleupon_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/delicious_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/furl_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/technorati_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/google_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/myspace_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/facebook_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/yahoobuzz_48.png http://www.techtamasha.com/wp-content/plugins/sociofluid/images/twitter_48.png


Leave a Reply