Kichu
Kichu

Reputation: 3267

Checking OK button is clicked on alert box

This the code for alerting some value:

alert('Click the OK button Now !');

So now i want to check whether the OK button is clicked or not.

How can I do this using this JavaScript?

Upvotes: 2

Views: 25115

Answers (4)

Paul Oliver
Paul Oliver

Reputation: 7671

Confirm could work:

var r=confirm("Click the OK button now!");
if (r==true)
{
  alert("You pressed OK!");
}
else
{
  alert("You pressed Cancel!");
}

Confirm HAS to have an OK and Cancel button. If you only want one button, you should either use the alert() method (which doesn't tell you if the OK was clicked) or you should look into something like the jQueryUI Dialog control.

The jQueryUI dialog is a bit more complicated because you need to include some extra JavaScript libraries and do a bit of extra wiring up to get it to work. There are a lot of examples to follow here.

Upvotes: 9

Scryptronic
Scryptronic

Reputation: 111

As mentioned previously, confirm() is the best bet, however don't forget you can check which button was pressed, and ask for a value at the same time using prompt().

if (prompt("Click the OK button?")!=null)
{
alert('you clicked OK and entered a value')
}
else
{
alert('you clicked cancel')
}

Upvotes: 1

Scott
Scott

Reputation: 285

Use a jquery dialog then you can post or check what every you want from a range of buttons. Much more flexible

Jquery Modal

Upvotes: 1

DanS
DanS

Reputation: 18463

Sounds like you might want a confirm box instead of an alert:

http://www.w3schools.com/js/js_popup.asp

This returns true or false depending on what the user presses. Alert does not return a value.

Upvotes: 2

Related Questions