Submit your widget

custom confirm dialog in the form use jQuery plugin

Created 9 years ago   Views 27042   downloads 3550    Author tutorialzine
custom confirm dialog in the form use jQuery plugin
View DemoDownload 22.8 KB
Share |

Continuing on our quest for building customizable alternatives for browser’s controls, this week we are building a cross-browser, custom confirm dialog in the form of an easy to use jQuery plugin. You can choose the text, buttons, and actions that will be executed when they are clicked.


Although we are mainly concentrating on the confirmation dialog, lets first say a few words about the page on which we are going to use it. If you are eager to see the plugin’s source code, you can skip this step and scroll down to the jQuery part of the tutorial.


<!DOCTYPE html>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>A jQuery Confirm Dialog Replacement with CSS3 | Tutorialzine Demo</title>

<!-- Including the Cuprum font with @font-face from Google's webfont API  -->

<link href='' rel='stylesheet' type='text/css'>
<link rel="stylesheet" type="text/css" href="css/styles.css" />
<link rel="stylesheet" type="text/css" href="jquery.confirm/jquery.confirm.css" />


<div id="page">

        <div class="item">
            <a href="">
             <img src="" title="Coding a Rotating Image Slideshow w/ CSS3 and jQuery" alt="Coding a Rotating Image Slideshow w/ CSS3 and jQuery" width="620" height="340" />
            <div class="delete"></div>

        <!-- Other "item" divs -->


<!-- Including jQuery and our jQuery Confirm plugin -->

<script src=""></script>
<script src="jquery.confirm/jquery.confirm.js"></script>
<script src="js/script.js"></script>


In the head section of the document, I am including the Cuprum font from Google’s Font Directory, jquery.confirm.css, which is the stylesheet for the confirm dialog, and styles.css, which styles the example page.

At the bottom of the body section, I’ve included the jQuery library, jquery.confirm.js, which is the main plugin file, and script.js, which listens for the click event on the example page uses fires up the plugin. These last two files are discussed in the final step of this tutorial.

The dialog box itself is nothing more than a few lines of HTML. Bellow you can see the code that is inserted by the plugin to display the confirm window.

Example code for the dialog

<div id="confirmOverlay">
    <div id="confirmBox">

        <h1>Title of the confirm dialog</h1>
        <p>Description of what is about to happen</p>

        <div id="confirmButtons">
            <a class="button blue" href="#">Yes<span></span></a>
            <a class="button gray" href="#">No<span></span></a>

This code is appended to the body of the document. The confirmOverlay is displayed over the rest of the page, preventing any interactions with it while the dialog is visible (modal behavior). The h1, p and the confirmButtons div are populated according to the arguments you pass to the plugin. You will read more on that later on in this article.


The styling of the confirm dialog is contained in jquery.confirm.css. This makes it easier to include into an existing project, and it is built in such a way, that you can be sure it will not clash with the rest of your page styles.


 background: -moz-linear-gradient(rgba(11,11,11,0.1), rgba(11,11,11,0.6)) repeat-x rgba(11,11,11,0.2);
 background:-webkit-gradient(linear, 0% 0%, 0% 100%, from(rgba(11,11,11,0.1)), to(rgba(11,11,11,0.6))) repeat-x rgba(11,11,11,0.2);

 background:url('body_bg.jpg') repeat-x left bottom #e5e5e5;
 margin:-130px 0 0 -230px;
 border: 1px solid rgba(33, 33, 33, 0.6);

 -moz-box-shadow: 0 0 2px rgba(255, 255, 255, 0.6) inset;
 -webkit-box-shadow: 0 0 2px rgba(255, 255, 255, 0.6) inset;
 box-shadow: 0 0 2px rgba(255, 255, 255, 0.6) inset;

#confirmBox h1,
#confirmBox p{
 font:26px/1 'Cuprum','Lucida Sans Unicode', 'Lucida Grande', sans-serif;
 background:url('header_bg.jpg') repeat-x left bottom #f5f5f5;
 padding: 18px 25px;
 text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.6);

#confirmBox h1{

#confirmBox p{
 padding-top: 35px;

 padding:15px 0 25px;

#confirmBox .button{
 background:url('buttons.png') no-repeat;
 height: 33px;

 font:17px/33px 'Cuprum','Lucida Sans Unicode', 'Lucida Grande', sans-serif;

 margin-right: 15px;
 padding: 0 35px 0 40px;

#confirmBox .button:last-child{ margin-right:0;}

#confirmBox .button span{
 background:url('buttons.png') no-repeat;

#confirmBox .blue{    background-position:left top;text-shadow:1px 1px 0 #5889a2;}
#confirmBox .blue span{   background-position:-195px 0;}
#confirmBox .blue:hover{  background-position:left bottom;}
#confirmBox .blue:hover span{ background-position:-195px bottom;}

#confirmBox .gray{    background-position:-200px top;text-shadow:1px 1px 0 #707070;}
#confirmBox .gray span{   background-position:-395px 0;}
#confirmBox .gray:hover{  background-position:-200px bottom;}
#confirmBox .gray:hover span{ background-position:-395px bottom;}

A handful of CSS3 declarations are used. In the #confirmOverlay definition, we are using CSS3 gradients (only available in Firefox, Safari and Chrome for now), with a transparent PNG fallback for the rest.

Later, in the #confirmBox, which is centered in the middle of the screen, I’ve added an inset box-shadow, which is in effect a highlight (think of Photoshop’s inner glow). Also I’ve used the Cuprum font, which was included from Google’s Font Directory.

Along with some text shadows, you can see the styling of the buttons. They are built using the sliding doors technique. Currently two designs are available – blue and gray. You can add a new button color by specifying additional declarations.


Before moving to the source code of the plugin, lets first see what we are after. In script.js you can see how the plugin is called.



 $('.item .delete').click(function(){

  var elem = $(this).closest('.item');

   'title'  : 'Delete Confirmation',
   'message' : 'You are about to delete this item. <br />It cannot be restored at a later time! Continue?',
   'buttons' : {
    'Yes' : {
     'class' : 'blue',
     'action': function(){
    'No' : {
     'class' : 'gray',
     'action': function(){} // Nothing to do in this case. You can as well omit the action property.



When the .delete div is clicked in our example page, the script is executes the $.confirm function,  defined by our plugin. It then passes the title of the dialog, the description, and an object with the buttons. Each button takes the name of a CSS class and an action property. This action is the function that is going to be executed when the button is clicked.

Now lets move to the interesting part. In jquery.confirm.js you can see the source code of our confirm dialog alternative.



 $.confirm = function(params){

   // A confirm is already shown on the page:
   return false;

  var buttonHTML = '';

   // Generating the markup for the buttons:

   buttonHTML += '<a href="#" class="button '+obj['class']+'">'+name+'<span></span></a>';

    obj.action = function(){};

  var markup = [
   '<div id="confirmOverlay">',
   '<div id="confirmBox">',
   '<div id="confirmButtons">',


  var buttons = $('#confirmBox .button'),
   i = 0;


    // Calling the action attribute when a
    // click occurs, and hiding the confirm.

    return false;

 $.confirm.hide = function(){


Our plugin defines the $.confirm() method. What it does, is read the arguments you’ve passed, construct the markup, and then add it to the page. As the #confirmOverlay div is assigned a fixed positioning in its CSS declaration, we can leave it to the browser to center it on the screen and move it when the visitor scrolls the page.

After adding the markup, the script assigns event handlers for the click events, executing the action parameter for the respective button.

With this our jQuery-powered Confirm Dialog is complete!

Wrapping it up

You can customize the appearance of the dialog by modifying jquery.confirm.css. As the message attribute of the dialog takes HTML text, you can further customize it by displaying images and icons in the confirm window.

You can even alternatively use this plugin as an alert dialog – you will just need to pass a single button with no action attribute.

Tag: form, dialog