21

How to write a confirm dialog in JavaScript with custom choices?

Instead of just "Ok" and "Cancel", I would like to have for example "This" "That" and "Other".

Ivar
  • 6,138
  • 12
  • 49
  • 61
TheOne
  • 10,819
  • 20
  • 81
  • 119
  • Possible duplicate of [how to show confirmation alert with three buttons 'Yes' 'No' and 'Cancel' as it shows in MS Word](http://stackoverflow.com/questions/9091001/how-to-show-confirmation-alert-with-three-buttons-yes-no-and-cancel-as-it) – David Millar Mar 08 '17 at 21:43
  • 1
    Regarding possible duplicate - I'd like to point out that this question was written two years earlier ;) – Karol Selak Aug 02 '17 at 18:10

4 Answers4

21

In short, you can't.

You might want to consider looking into using something like a jQuery UI dialog instead.

Jordan Ryan Moore
  • 6,877
  • 2
  • 26
  • 27
4

You could ask the user for an answer using:

var userChoice = prompt("Question");

You could loop that sentence until the user enters an answer within the valid ones.

Isidro Moran
  • 322
  • 2
  • 6
2

You can't. Use some javascript UI (jQuery UI, YUI, Mootools) library and mimic a dialog you need.

NilColor
  • 3,462
  • 3
  • 30
  • 44
1
// custom Confirm builder
function OnConfirm(text, func) {

    var _confirm = $('<div/>').addClass('confirm');
    _confirm.append($('<h2/>').text(text));

    _confirm.append('<br/><br/>');

    var _btnCancel = $('<input/>').attr('type', 'button').val('cancel')
        .bind('click', function () {
            $(this).parent('.confirm').hide();
            func(false);
        });

    var _btnApply = $('<input/>').attr('type', 'button').val('OK')
        .bind('click', function () {
            $(this).parent('.confirm').hide();
            func(true);
        });

    _confirm.append(_btnCancel);
    _confirm.append(_btnApply);
    $('body').append(_confirm);
}

$(function () { // documen.loaded
    $('#testLink').click(function (e) {
        e.preventDefault(); ;
        var _href = $(this).attr('href');
        var _title = $(this).attr('title');
        // call custom confirm function with callback function
        OnConfirm(_title, function (_isContinue) {               
                if (_isContinue) {
                    location.href = _href;
                }
            }
        );
    });
});
Alec Kobi
  • 11
  • 3