Developing a personalized pop-up notification using jQuery and CSS

Fiddle

 $(document).ready(function () {
     $('.del').click(function () {

         // var id = $(this).attr('cmnt-id');
         // $.post(url,{
         //data sent
         //},function(data){
         //remove the comment div

         //show the confirm box and ask for that question now
         confirmBox("Sure wanna delete this?");
         $('#console').append('deleted<br />');
         //});
     });

 });

 function confirmBox(text) {
     var c = $('.confirm');
     c.children('.confirm-text').text(text);
     c.show();
 }

Before deleting upon clicking, I need to show a confirmation message. Once confirmed, the action will be executed by displaying 'delete' in the console.

I prefer not to use any custom browser popups like confirm() or additional plugins.

However, I'm unsure how to integrate the confirm box functionality effectively. Any assistance would be appreciated!

UPDATE

If there is an element such as:

<button data-id="10" id="send">SEND</button>

jQuery

$('.send').click(){
   var data_id = $(this).attr('data-id');

   $.post(url{
       data_id : data_id
   },function(){data}{

     $('#console').append('sent');    

   });
};

In this scenario, how can I implement the confirmbox function properly?

Answer №1

Set up event listeners to handle clicks on the Yes and No buttons in your confirmation box.

Here's an example using jQuery:

$('.confirm > .yes').click(function () {
    $('#console').append('deleted<br />');

//DO SOMETHING WHEN 'YES' IS CLICKED

    $('.confirm').hide();
});
$('.confirm > .no').click(function () {

//DO SOMETHING WHEN 'NO' IS CLICKED

    $('.confirm').hide();
});

UPDATE // for reusability

We've updated the confirmBox() function to accept two parameters: (the text to display in the confirm box, the function to execute when Yes is clicked).

Example:

function confirmBox(text, yesFunc) {
    var c = $('.confirm');
    c.children('.confirm-text').text(text);
    c.show();
    $('.confirm > .yes').click(function () {
         yesFunc();
         c.hide();
         yesFunc = function () {};
    });    
    $('.confirm > .no').click(function () {
         c.hide();
         yesFunc = function () {};
    });
}

Now you can call the confirmBox() function within event handlers for different buttons (e.g., delete, create, etc.) and pass the corresponding actions/functions.

EXAMPLES:

$('.del').click(function () {
    confirmBox("Are you sure you want to delete this?", function () {
    $('#console').append('deleted<br />') //or other function
    });
});

$('.cre').click(function () {
    confirmBox("Are you sure you want to create this?", function () {
    $('#console').append('created<br />'); //or other function
    });
});

$('.send').click(function () {
    confirmBox("Are you sure you want to send this?", function () {
    $('#console').append('sent<br />'); //or other function 
    });
});

VIEW WORKING DEMO

Answer №2

Enhanced the confirm box to be reusable with jQuery 1.9.1:

$(document).ready(function () {
     $('.del').click(function () {
         //Confirmation box for delete action with callback function
         confirmBox("Are you sure you want to delete this?", function () {
             $('#console').append('deleted<br />');
             $('.confirm').hide();
         });
     });
     $('.cre').click(function () {
         // Confirmation box for append action with calling function
         confirmBox("Are you sure you want to append this?", function () {
             $('#console').append('appended<br />');
             $('.confirm').hide();
         });
     });
     // No button event handling
     $('.no').click(function () {
         $('.confirm').hide();
     });
 });

 // Updated confirmBox function now accepts a callback function for yes button click event
 function confirmBox(text, callback) {
     var c = $('.confirm');
     c.children('.confirm-text').text(text);
     c.show();
     $(document).off('click', '.yes');           // Clear previous click event listener
     $(document).on('click', '.yes', callback);  // Set up new click event listener
 }

View working code at:

JSFiddle

Answer №3

$(document).ready(function () {
     $('.del').click(function () {
         confirmBox("Are you sure you want to delete this?", function(){
           // var id = $(this).attr('cmnt-id');
           // $.post(url,{
           //data sent
           //},function(data){
           //remove the comment div

           //show the confirmation box and ask for confirmation now
           $('#console').append('deleted
'); //}); }); }); }); function confirmBox(text, yesFunc) { var c = $('.confirm'); c.children('.confirm-text').text(text); c.show(); $(".yes").unbind("click").click(function(){yesFunc(); c.hid();}); $(".no").click(function(){c.hide();}); }

Answer №4

To make it work, simply link the delete feature to the 'yes' button.

Check out this JSFiddle example: http://jsfiddle.net/jj64k/7/

 $('.yes').click(function () {
         $('#log').append('Item deleted successfully<br />');
         $('.confirmMsg').hide();

     });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Binding an event to an Angular 2 component directly within its selector code

Looking at my Angular 2 component: import { Component, ElementRef, Renderer } from '@angular/core';; @Component({ selector: 'my-button', templateUrl: 'button.html' }) export class ButtonComponent { private text: string ...

Creating a login system in Node.js with the help of Express

I'm currently working on building a login feature for a website using node.js with the express framework. The code seems to be running in an unexpected order and I'm having trouble figuring out how to resolve it. Below is a simplified version of ...

What is the best way to display data stored in local storage using React and Typescript?

Is there a way for the data to be displayed on the browser below the save button when I click save? My setup involves using React with a TypeScript template. function ButtonDefaultExample(props: IButtonExampleProps) { const { disabled, checked } = pro ...

Using DataTable with backend processing

Has anyone successfully implemented pagination in DataTable to dynamically generate the lengthMenu (Showing 1 to 10 of 57 entries) and only load data when the next page is clicked? I'm currently facing this challenge and would appreciate some guidance ...

How can Conditional Rendering be applied within List Rendering (v-for) in Vue JS?

In my current project, I am utilizing List Rendering (v-for) to display information about various books stored in an array. One challenge I'm facing is implementing conditional rendering within this loop to dynamically generate li elements that repre ...

Attempting to configure the SCSS compiler in Broccoli.js and Ember-CLI

I've been struggling to integrate Foundation into my Ember-CLI app using broccoli-scss for compiling all the SCSS files. Despite following the structure from the broccoli sample app, I just can't seem to get it working. My approach involves havi ...

What is the purpose of using route('next') within ExpressJS?

In the documentation it states: You can have multiple callback functions that act as middleware and can invoke next('route') to skip the remaining route callbacks. This is useful for setting pre-conditions on a route and then passing control t ...

"Angular's ui-router allows for nested views with individual templates and controllers for each

I have the following list of files: index.html car.html truck.html mainCtrl.js carCtrl.js truckCtrl.js I aim to create the following routes: #/search (template: index.html, controller: mainCtrl.js) #/search/car (template: car.html, controller: carCtrl. ...

Combining items in JavaScript using a shared key

Is there a way to combine 4 separate arrays of objects into one big object based on the keys inside an object? For example: OUTPUT: What I want to achieve. [ { "bugId": "", "testerId": "", "firstName": "", "lastName": "", "country": " ...

An unusual phenomenon in the controller hierarchy causing issues with accessing the parent scope in AngularJS

Recently delving into the world of angularjs, I encountered something perplexing that seemed like a glitch in the controller hierarchy when it comes to accessing the parent scope. Here are two code snippets that I believed should both function properly bas ...

The presented data in the HTML table is displayed in a reverse horizontal order

I am in the process of developing a calculator and have chosen to use Bootstrap4 tables for the button alignment. However, I am facing an issue where the table is being rendered in the reverse order of the code that I have written. The columns within the t ...

Issue encountered when attempting to display JSON index on individual result page

This is a JSON response. { "listing": { "id": "1", "name": "Institute Name", "contact": "9876543210", "website": "http://www.domain.in", "email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" da ...

Why won't applying a binding style affect the appearance of my Vue component?

const EventLevelBoard = { name: "EventLevel", data: { levelStyle: { display: "block" }, levelStyleinner: [ { display: "block" }, { display: "block" }, { display: "block&qu ...

Expand Navigation Bar upon Hover

My goal is to recreate a Navigation Bar similar to the one found on this website: I want the Navigation Block to expand and show Menu Items when hovered over, just like the example on the website mentioned above. I've attempted to use Bootstrap and ...

The hidden overflow property in CSS and floating elements

I need help creating a list of items with a cross icon in the middle on the right-hand side. I tried using an anchor with padding, floated it to the right, and relatively positioned it. The width and height of the icon are known. Each item should also hav ...

Error: The function could not be located

My HTML page is quite straightforward: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name=&quo ...

PHP not delivering a variable via AJAX

I've noticed that there are similar questions on this platform, but I've spent my entire day researching and fixing bugs to figure out why my ajax code doesn't return a response from the php file. All I need is for it to notify me when a use ...

The combination of eq, parent, and index appears to be ineffective

Okay, so I have this table snippet: <tr> <td> <input type="text" name="value[]" class="value" /> </td> <td> <input type="text" name="quantity[]" class="quantity" /> </td> </tr> Here& ...

I've noticed that the NextJs router appears to be significantly slower in comparison to React

I currently have a website that is built in both React Js and Next Js. The issue I am currently encountering is that the router in NextJs is noticeably slower compared to react-router-dom. It takes almost 2-3 seconds to change the route. To experience th ...

How can I troubleshoot jQuery not functioning in iOS even though it works on Safari?

Trying to implement jQuery on a website using xCode simulator for testing, but experiencing issues. Works fine on Safari webkit though. Click here to view the site. I would appreciate finding out what's causing the problem, as well as advice on how t ...