Pages Navigation Menu

Coding is much easier than you think

How to use JQuery dialog as Confirm dialog?

There are quite a few articles you might have read on SimpleCodeStuffs on jQuery like jQuery Tutorial for Beginners, AJAX implementation in Servlets using jQuery and jQuery UI etc.
This example will help you if you have any one of below queries:

  • How to implement “confirmation” dialog in jQuery UI dialog?
  • How to use jQuery UI dialog as JavaScript confirm BOX?
  • How to use JQuery dialog as Confirm dialog?
  • jQuery UI Replacement for alert()

Issue: How many times did you have to code some confirmation logic on your website to ensure that your users are sure about the action they are about to perform?
A typical example is prompting the user to confirm that they are sure about deleting a record. In typical/boring JavaScript world, you would do something like this:






function deleteConfirmation() {  
    return confirm("Are you sure you want to delete this item?");

There is nothing wrong with this above code. However, it is plain. You cannot customize it with CSS make it look consistent with your site’s colour and identity.

jQuery to the rescue

jQuery UI has the solution for you. You can use the jQuery UI Dialog to present users with customized confirmation/dialog boxes as shown below.
Let’s see how to implement this


Firstly, let’s add the necessary jQuery libraries in the head section of the Jsp page.




File: confirmJs.js

    $('#Delete').click(function(event) {
	var currentForm = $(this).closest('form');
	/** Create div element for delete confirmation dialog*/
	var dynamicDialog = $('
'+ ''+ 'Are you sure to delete the item?
'); dynamicDialog.dialog({ title : "Are you sure?", closeOnEscape: true, modal : true, buttons : [{ text : "Yes", click : function() { $(this).dialog("close"); currentForm.submit(); } }, { text : "No", click : function() { $(this).dialog("close"); } }] }); return false; }); });

The code above instantiates the dialog box and assigns it two buttons. The”Yes”and”No”buttons. The “Yes” is responsible for submitting the form to the server.

Other jQuery tutorial from our Blog



jQuery closest() Method

Leave a Comment

Your email address will not be published. Required fields are marked *