javascript-confirm 1.0

A lightweight, customizable JavaScript library for modern confirmation dialogs without jQuery.

Download v.1.0.1 View on GitHub

Welcome to javascript-confirm

JavaScript Confirm is a lightweight, customizable library that replaces the default browser confirmation dialog with a sleek, modern UI. It’s easy to integrate into any project and doesn’t require jQuery, making it ideal for minimalistic setups. Compatible with AMD, CommonJS, and browser globals.

Request Feature Post an Issue

Getting Started

To get started with JavaScript Confirm plugin, follow these steps:

Once you've downloaded the plugin, place the javascript-confirm.min.js file in your project's JavaScript directory (e.g., /js/).
<script src="assets/js/javascript-confirm.min.js"></script>

or using cdn

<script src="https://cdn.jsdelivr.net/gh/talentedaamer/javascript-confirm/dist/javascript-confirm.min.js"></script>

Usage & Examples

See javascriptConfirm in action with examples demonstrating its functionality.

Confirmation
Results
javascriptConfirm(document.getElementById("example_1"), {
    onConfirm: function() {
        // do something
    },
});
Confirm / Cancel
Results
javascriptConfirm(document.getElementById("example_2"), {
    onConfirm: function() {
        // on confirm do something
    },
    onCancel: function() {
        // on cancel do something
    },
});
Title & Message
Results
javascriptConfirm(document.getElementById("example_3"), {
    title: "Delete User",
    message: "Do you want to delete this user?",
    onConfirm: function() {
        // delete user
    },
    onCancel: function() {
        // do nothing
    },
});
All Options
Results
javascriptConfirm(document.getElementById("example_4"), {
    title: "Delete User",
    message: "Do you want to delete this user?",
    confirmText: "Delete",
    confirmClass: "btn btn-sm btn-primary",
    cancelText: "Cancel",
    cancelClass: "btn btn-sm btn-danger",
    cancelOnBackdropClick: true,
    width: '400px',
    onConfirm: function() {
        // delete user
    },
    onCancel: function() {
        // do nothing
    },
});

API

javascriptConfirm(element, options)

This function initializes and displays a confirmation dialog, allowing you to customize the behavior, appearance, and interactions. It is designed to work in browsers and various module environments (e.g., AMD, CommonJS).

Parameters

  • element: (HTMLElement, required):

    The DOM element to which the confirmation dialog is applied. This must be a valid HTML element. If the element is invalid or not provided, an error will be logged, and the function will exit early.

  • options: (Object, required):

    Configuration options for customizing the confirmation dialog. If an invalid or missing options object is passed, an error will be logged.

Options

You can customize the confirmation dialog using the following options:

Option Description
title: string The title displayed at the top of the confirmation dialog. Default value: "".
message: string The message text shown in the confirmation dialog. Default value: "Are you sure?".
confirmText: string The label of the "confirm" button. Default value: "Confirm".
confirmClass: string Custom CSS classes applied to the "confirm" button to control its appearance. Default value: "jc-btn jc-success".
cancelText: string The label of the "cancel" button. Default value: "Cancel".
cancelClass: string Custom CSS classes applied to the "cancel" button to control its appearance. Default value: "jc-btn jc-danger".
cancelOnBackdropClick: boolean Determines whether clicking on the backdrop (outside the dialog) will cancel the action and close the dialog. Default value: false.
width: string Sets the width of the confirm dialog. Accepts any valid CSS unit (e.g., px, em, rem, vw, %). Default value: '300px'.
onConfirm: function Callback function executed when the user confirms the action. Default: an empty function function() {}.
onCancel: function Callback function executed when the user cancels the action. Default: an empty function function() {}.