Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom "confirm" dialog in JavaScript?

I've been working on an ASP.net project that uses custom 'modal dialogs'. I use scare quotes here because I understand that the 'modal dialog' is simply a div in my html document that is set to appear "on top" of the rest of the document and is not a modal dialog in the true sense of the word.

In many parts of the web site, I have code that looks like this:

var warning = 'Are you sure you want to do this?'; if (confirm(warning)) {     // Do something } else {     // Do something else } 

This is okay, but it would be nice to make the confirm dialog match the style of the rest of the page.

However, since it is not a true modal dialog, I think that I need to write something like this: (I use jQuery-UI in this example)

<div id='modal_dialog'>     <div class='title'>     </div>     <input type='button' value='yes' id='btnYes' />     <input type='button' value='no' id='btnNo' /> </div>  <script> function DoSomethingDangerous() {     var warning = 'Are you sure you want to do this?';     $('.title').html(warning);     var dialog = $('#modal_dialog').dialog();     function Yes() {         dialog.dialog('close');         // Do something     }        function No() {         dialog.dialog('close');         // Do something else     }         $('#btnYes').click(Yes);     $('#btnNo').click(No); } 

Is this a good way to accomplish what I want, or is there a better way?

like image 772
Vivian River Avatar asked Aug 03 '11 15:08

Vivian River


People also ask

What is confirm () function in JavaScript?

Definition and Usage. The confirm() method displays a dialog box with a message, an OK button, and a Cancel button. The confirm() method returns true if the user clicked "OK", otherwise false .

How can write confirm box in JavaScript?

Syntax. window. confirm("sometext"); The window.

How do I get confirmation in JavaScript?

You can create a JavaScript confirmation box that offers yes and no options by using the confirm() method. The confirm() method will display a dialog box with a custom message that you can specify as its argument.

Can we style confirm box?

confirm(); alerts cannot be styled, they are generic to the browser.


2 Answers

You might want to consider abstracting it out into a function like this:

function dialog(message, yesCallback, noCallback) {     $('.title').html(message);     var dialog = $('#modal_dialog').dialog();      $('#btnYes').click(function() {         dialog.dialog('close');         yesCallback();     });     $('#btnNo').click(function() {         dialog.dialog('close');         noCallback();     }); } 

You can then use it like this:

dialog('Are you sure you want to do this?',     function() {         // Do something     },     function() {         // Do something else     } ); 
like image 182
alnorth29 Avatar answered Sep 18 '22 11:09

alnorth29


SweetAlert

You should take a look at SweetAlert as an option to save some work. It's beautiful from the default state and is highly customizable.

Confirm Example

sweetAlert(   {     title: "Are you sure?",     text: "You will not be able to recover this imaginary file!",     type: "warning",        showCancelButton: true,        confirmButtonColor: "#DD6B55",     confirmButtonText: "Yes, delete it!"   },    deleteIt() ); 

Sample Alert

like image 45
Joshua Pinter Avatar answered Sep 17 '22 11:09

Joshua Pinter