views:

389

answers:

3

I am displaying a javascript confirm-box message when user clicks on "Delete Data" button. I am displaying it as shown in this image:

alt text

In the image "OK" button is selected by default, I want to select "Cancel" button by default, so that accidently if user presses enter then records will be safe and will not be deleted.

Is tehre any way in javascript to sleect "Cancel" button by default??

A: 

I don't think you can do that.

But you can definitely make it harder for the user to accidentally 'agree' with the confirmation, for example by forcing the user to type 'y' in an input box:

agree = (prompt("Type 'y' to accept", '') == 'y');
Lukman
-1 Javascript `prompt` is the devil.
Josh Stodola
+4  A: 

If you can use jQuery plugin then here is a nice one

jQuery Impromptu

To change the default focused button:

$.prompt('Example 4',{ buttons: { Ok: true, Cancel: false }, focus: 1 });
rahul
Any reason for the down vote?
rahul
+1  A: 

You can't do that, but you could use/write your own dialog which is displayed using DOM elements (like phoenix suggested, it just doesn't have to be that particular jQuery plugin, you could write your own or use plugin from another JS framework).

The "use jQuery + plugin X" answers are starting to get annoying. There are a lot of JS libraries out there and even more plugins. For example using any JS library here is unnecessary if you just want to display a custom dialog. While it's a quick solution/answer, answers like that do more harm in a long run then good. People new to JavaScript or programing in general start to think that jQuery and/or plugins are the only way to go and they include 50kb+ library just to write 3 lines of their own (which sometimes don't even use the library :D).

In my opinion Markus Johnsson comment is the best answer, it's a shame it's not posted as one.

Maiku Mori