JQUERY-CONFIRM

alerts, confirms and dialogs in one.


Welcome to jquery-confirm!

A jQuery plugin that provides great set of features like, Auto-close, Ajax-loading, background-dismiss, themes and more.
This plugin is actively developed, We would love you have your suggestions.


Practical Uses

These features can practically be used like so.

Elegant Alerts.

Stacked Confirmations

Important modal

Need input?

Its also a Dialog.

Something huge?

User friendly?

Keyboard actions?

Installation

Download
Download the Latest version and use files in the /dist/ folder.

via Bower:
$ bower install craftpip/jquery-confirm

via Npm:
$ npm install jquery-confirm

Quick Usage

Dependencies:

  • Bootstrap by Twitter >= v2
  • jQuery library > v1.8

Show an alert dialog
$.alert() Has only confirm button.

$.alert({
    title: 'Alert!',
    content: 'Simple alert!',
    confirm: function(){
        alert('Confirmed!');
    }
});

Show an confirm dialog
$.confirm() Has both confirm & cancel buttons.

$.confirm({
    title: 'Confirm!',
    content: 'Simple confirm!',
    confirm: function(){
        alert('Confirmed!');
    },
    cancel: function(){
        alert('Canceled!')
    }
});

Show an modal dialog
$.dialog() Hides both confirm & cancel buttons. (Shows close [x] button)

$.dialog({
    title: 'Text content!',
    content: 'Simple modal!',
});

Examples

NOTE: The $.confirm(), $.dialog() & $.alert() methods are alias of jconfirm().
All three methods indirectly call the jconfirm base function altering the provided options.

BUTTON TEXT

Change the button text for confirm and cancel.

$.confirm({
    confirmButton: 'Yes i agree',
    cancelButton: 'NO never !'
});

BUTTON STYLE

Apply the classes you want in the buttons.

Available bootstrap options are btn-primary btn-inverse btn-warning btn-info btn-danger btn-success

$.confirm({
    confirmButtonClass: 'btn-info',
    cancelButtonClass: 'btn-danger'
})

HIDE ELEMENTS.

Hide the Title/Content/Confirm Button/Cancel Button/Close icon when u don't need them.

$.confirm({
    title: false, // hides the title.
    cancelButton: false // hides the cancel button.
    confirmButton: false, // hides the confirm button.
    closeIcon: false, // hides the close icon.
    content: false, // hides content block.
});
NOTE:

By default the closeIcon is visible if both confirm & cancel buttons are hidden. (dialog mode).
To explicitly show or hide closeIcon set closeIcon: true or closeIcon: false.
Shorthand to hide both buttons is to use $.dialog().

THEME

The Light & Dark themes that suit any website design,

$.confirm({
    theme: 'white'
});
$.confirm({
    theme: 'black'
});
$.confirm({
    theme: 'hololight'
});
$.confirm({
    theme: 'holodark'
});
$.confirm({
    theme: 'supervan'
});

CUSTOM WIDTH

Jquery-confirm uses bootstrap's grid system for its layout.
You can simply provide column classes to adjust the modal's width.

You can also set responsive layouts. Bootstrap grid docs

$.confirm({
    columnClass: 'col-md-4 col-md-offset-4'
});
$.confirm({
    columnClass: 'col-md-4'
});
$.confirm({
    columnClass: 'col-md-4 col-md-offset-8 col-xs-4 col-xs-offset-8'
});

LOAD VIA URL

Get content from a file/url.
Prepend URL: to your URL. The URL will be called with the get ajax method.
Example content: "URL:http://example.com/getData?id=1"

The confirm/cancel buttons are disabled until the content is fetched from the URL.

You can also load HTML contents to the modal with ease.

view text.txt
$.confirm({
    content: 'url:text.txt',
    title: 'Title'
}); 

// Using contentLoaded callback.
$.confirm({
    content: 'url:text.txt',
    title: 'Title',
    contentLoaded: function(data, status, xhr){
        var that = this;
        setTimeout(function(){
            that.setContent('Amazing huh?: '+status);
        }, 1000);
    }
}); 

contentLoaded is called regardless of success or error.

LOAD VIA URL (Advanced Option)

Get full control over Ajax calls and its callbacks.
Pass a function that returns jquery ajax promise. i.e. $.get(), $.post(), $.ajax(), etc.

$obj is the reference object to jconfirm, and provides set of methods.

view bower.json
$.confirm({
    content: function ($obj) {
        return $.ajax({
            url: 'bower.json',
            dataType: 'json',
            success: function (data) {
                $obj.setContent('Plugin description: '+data.description); // sets the content and centers the dialog on screen.
                // Ahh, success!
            },
            error: function () {
                $obj.contentDiv.html("Something went wrong, please try again later."); // sets the content straightaway.
                // Handle it your way
            }
        });
    },
    confirm: function(){
        // here this.content is the current content overwritten everytime new content is set.
        this.setContent(this.content+'<h4>Adding a new sentence.</h4>');
        return false;
    }
});

ANIMATION!

All the impression lies in what we see.
Lots and lots of animations

2D animations:

3D animations:

$.confirm({
    animation: 'zoom'
});

see options for list of options you can apply.

ANIMATION BOUNCE

Some eye candy thats in fashion.

$.confirm({
    animationBounce: 1.5, // default is 1.5 whereas 1 is no bounce.
});

ANIMATION SPEED

Adjust the duration of animation.

$.confirm({
    animationSpeed: 2000 // 2 seconds
});
$.confirm({
    animationSpeed: 200 // 0.2 seconds
}); 

AUTO-CLOSE

Do a action if the user does not respond of specified time.
The autoClose option takes in a string, like 'confirm|4000' where confirm is the action to trigger after 4000 milliseconds.
Practical examples of autoClose

$.confirm({
    title: 'Delete user?',
    content: 'This dialog will automatically trigger \'cancel\' in 6 seconds if you don\'t respond.',
    autoClose: 'cancel|6000',
    confirm: function(){
        alert('confirmed');
    },
    cancel:function(){
        alert('canceled');
    }
});

$.confirm({
    title: 'Logout?',
    content: 'Your time is out, you will be automatically logged out in 10 seconds.',
    autoClose: 'confirm|10000',
    confirm: function(){
        alert('confirmed');
    },
    cancel:function(){
        alert('canceled');
    }
});

ICONS

Give meaning to your dialog with custom icons.
Read about Font Awesome here.

$.confirm({
    icon: 'glyphicon glyphicon-heart',
    title: 'glyphicon'
});
$.confirm({
    icon: 'fa fa-warning',
    title: 'font-awesome'
});
$.alert({
    icon: 'fa fa-spinner fa-spin',
    title: 'Working!',
    content: 'Sit back, we are processing your request. <br>The animated icon is provided by Font-Awesome!'
});

BACKGROUND DISMISS

By default the 'cancel' action is trigged if the user click outside of the dialog.

$.confirm({
    backgroundDismiss: true,
    content: 'Click outside the dialog'
});
$.confirm({
    backgroundDismiss: false,
    content: 'Click outside the dialog'
});

KEYBOARD ACTIONS

Enables keyboard events on jquery-confirm dialog.
ENTER calls confirm(); & ESC calls cancel();

$.confirm({
    keyboardEnabled: true,
    content: 'Press ESC or ENTER to see it in action.',
    cancel: function(){
        alert('canceled');
    },
    confirm: function(){
        alert('confirmed');
    }
});

GLOBAL DEFAULTS

You can setup global settings for your jconfirm.

jconfirm.defaults should be set after the plugin has loaded.

jconfirm.defaults = {
    title: 'Hello',
    content: 'Are you sure to continue?',
    contentLoaded: function(){
    },
    icon: '',
    confirmButton: 'Okay',
    cancelButton: 'Cancel',
    confirmButtonClass: 'btn-default',
    cancelButtonClass: 'btn-default',
    theme: 'white',
    animation: 'scale',
    animationSpeed: 400,
    animationBounce: 1.5,
    keyboardEnabled: false,
    container: 'body',
    confirm: function () {
    },
    cancel: function () {
    },
    backgroundDismiss: true,
    autoClose: false,
    closeIcon: null,
    columnClass: 'col-md-6 col-md-offset-3',
};

OPTIONS

Options, their defaults and possibilities.

Name type default description
title String 'Hello' Title of the dialog.
content String, Function 'Are you sure to continue?' Content for the dialog.
Or use a function that returns Ajax promise.
contentLoaded function function(){} (optionally available) If you load content via URL using 'url:http://abc.com/xyz', contentLoaded will be the callback.
icon String '' Icon class prepended before the title.
confirmButton String 'Okay' Button text for the confirm callback.
cancelButton String 'Cancel' Button text for the cancel callback.
confirmButtonClass String 'btn-default' Class for the confirm button.
cancelButtonClass String 'btn-default' Class for the cancel button.
theme String 'white' Color theme for the dialog.
possible options are 'white' & 'black'
animation String 'scale' Open & Close animation for the dialog.
possible options are 'scale', 'top', 'bottom', 'left', 'right', 'zoom', 'opacity', 'none', 'rotate', 'rotatex', 'rotatey', 'scalex', 'scaley'.
The 'blur' animation was removed in v1.1.2
animationSpeed Number 400 Animation duration in miliseconds.
animationBounce Float 1.5 Adds a Bounce open animation,
1 = No bounce
keyboardEnabled Boolean false Use the ENTER key to trigger the confirm action and ESC key to trigger the cancel action.
container String 'body' Specify where the generated HTML content for jconfirm should append.
By default it appends in the document's <body>.
confirm Function function(){} function to run after the user clicks the confirm button.
cancel Function function(){} function to run after the user clicks the cancel button.
backgroundDismiss Boolean true if the user can dismiss the dialog via clicking outside the dialog.
autoClose String false Auto-close the dialog within a specified time, if the user doesn't respond.
possible option 'confirm|400'

the string is divided in two halves with '|', the first part specifies the button to trigger, 'confirm' or 'cancel'. The other half specifies the wait time in miliseconds.

closeIcon Boolean null By default closeIcon is visible if both buttons are false. (dialog mode).
closeIcon can be shown by setting this value to a Boolean value.
columnClass String 'col-md-6 col-md-offset-3' Provides a better way to set Custom width to Jquery-onfirm.
You can also set custom widths for different display sizes using the Bootstraps grid.

RETURNED OBJECT & METHODS

The function var obj = $.confirm() returns a reference object on execution.
This obj can be used to interact with the opened dialog.

var obj = $.confirm({title: 'awesome'}); // initial execution

obj.$b & obj.contentDiv

The $b object is a jQuery DOM reference to the dialog. You can access your Dialogs contents via this object.

var obj = $.confirm({
    title: 'awesome',
    content: '<span class="styled">Yeah, this is awesome</span>'
})

var finding = obj.$b.find('.styled').html();
console.log(finding);
// Yeah, this is awesome

var contentBlock = obj.contentDiv.html();
console.log(contentBlock);
// <span class="styled">Yeah, this is awesome</span>

obj.close()

The close method closes/destroys the dialog.

var obj = $.confirm({
    ....
})
// Reference object is stored in 'obj', and the dialog opens on execution!

obj.close();
// The dialog is closed/destroyed.

obj.isClosed()

Tells if the dialog is closed or not.
Returns TRUE if closed else returns FALSE,

obj.setContent('text to set')

Overwrites the content in the dialog and centers the dialog on screen.

obj.setDialogCenter()

Centers the dialog on screen.

INSIDE CALLBACKS

Getting the reference object inside callback functions

You may need the returned reference obj inside the callback function, this is what you need.

var obj = $.confirm({
    title: 'awesome',
    content: 'Get me yeah.',
    confirm: function(){
        console.log(this.content);
        // Get me yeah.
    }
})

console.log(obj.content);
// Get me yeah.

// Moral, this within the callback function is the returned object on execution.

Preventing the dialog from closing

At times you need the content inside dialog be validated, (For example, If you have a form inside, and the fields are required). You can prevent the modal close by simply doing return false;

var obj = $.confirm({
    title: 'awesome',
    content: 'Your name: <input type="text" />', // You can also LOAD the html data using LOAD FROM URL feature,
    confirm: function(){

        var val = this.$b.find('input').val();
        // there is a input element inside the content.

        if(val == ''){
        // if the value is empty, dont close the dialog. (and maybe show some error.)

            return false;
        }

    }

    // NOTE: You can return any falsy value to prevent close (such as '', null, undefined, 0).
});