12

Possible Duplicate:
how to create yes/no/cancel box in javascript instead of ok/cancel?

In a Confirm message box, how can I change the buttons to say "Yes" and "No" instead of "OK" and "Cancel"? Is there some way to accomplish this using jQuery/Javascript? Thanks in advance for any help.

Community
  • 1
  • 1
user300485
  • 525
  • 5
  • 11
  • 24
  • Thanks Donut. for making edit my question. – user300485 Jun 22 '11 at 20:16
  • @Felix: It is not an exact duplicate, as the answers to the previous question did not mention how to do this with JQuery, only that it is possible. The answers here could be merged in that question, though. – Paŭlo Ebermann Jun 22 '11 at 22:55
  • @PaloEbermann: Well, there is a link to jQuery dialog. I think we can expect from developers that they read documentation (if they know where to find it). It's still a duplicate, the question is about exactly the same issue. – Felix Kling Jun 22 '11 at 23:19

6 Answers6

14

Create your own confirm box:

<div id="confirmBox">
    <div class="message"></div>
    <span class="yes">Yes</span>
    <span class="no">No</span>
</div>

Create your own confirm() method:

function doConfirm(msg, yesFn, noFn)
{
    var confirmBox = $("#confirmBox");
    confirmBox.find(".message").text(msg);
    confirmBox.find(".yes,.no").unbind().click(function()
    {
        confirmBox.hide();
    });
    confirmBox.find(".yes").click(yesFn);
    confirmBox.find(".no").click(noFn);
    confirmBox.show();
}

Call it by your code:

doConfirm("Are you sure?", function yes()
{
    form.submit();
}, function no()
{
    // do nothing
});

You'll need to add CSS to style and position your confirm box appropriately.

Working demo: jsfiddle.net/Xtreu

gilly3
  • 87,962
  • 25
  • 144
  • 176
  • this not work any way please change code or give more description about this. – Jubin Patel Mar 07 '13 at 13:06
  • @jubinPatel - Here is a working demo: http://jsfiddle.net/Xtreu/ – gilly3 Mar 07 '13 at 20:00
  • 2
    this confirm box doesnt disable the rest of the screen as normal confirm box does. even when the confirm box appears , I am able to press the button "Delete My Data" again – ALBI Nov 05 '13 at 05:12
  • 2
    @ALBI - This update to my jsfiddle adds an overlay to hide the page: http://jsfiddle.net/Xtreu/269/ I added the overlay with JavaScript, but you could also add it to the HTML and show/hide it with JavaScript like this: http://jsfiddle.net/Xtreu/270/. – gilly3 Nov 05 '13 at 18:22
13

If you switch to the jQuery UI Dialog box, you can initialize the buttons array with the appropriate names like:

$("#id").dialog({
  buttons: {
    "Yes": function() {},
    "No": function() {}
  }
});
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210
Richard B
  • 1,581
  • 1
  • 15
  • 31
2

No, it is not possible to change the content of the buttons in the dialog displayed by the confirm function. You can use Javascript to create a dialog that looks similar.

leocrimson
  • 702
  • 1
  • 11
  • 25
Donut
  • 110,061
  • 20
  • 134
  • 146
2

There is the Jquery alert plugin

$.alerts.okButton = ' Yes ';
$.alerts.cancelButton = ' No ';
Rahul
  • 5,603
  • 6
  • 34
  • 57
Mike Vormwald
  • 2,280
  • 22
  • 29
2

As far as I know, it's not possible to change the content of the buttons, at least not easily. It's fairly easy to have your own custom alert box using JQuery UI though

Rahul
  • 5,603
  • 6
  • 34
  • 57
riku
  • 763
  • 4
  • 7
2

An example using jQuery UI dialog: http://jsfiddle.net/JAAulde/qqkGA/ as well as UI's own demo: http://jqueryui.com/demos/dialog/#modal-confirmation

JAAulde
  • 19,250
  • 5
  • 52
  • 63