19

I have a form that has a delete button, I would like to create a confirmation box that pop ups when the delete button is clicked. The delete button currently works. I have tried several things in javascript with no luck. I am using Angular.

Is this best approach for this?

Also, does anyone know of any examples for this, I have not found any that work.

$(document).ready(function(){
  $("form").validate();
  $(".radius small success button").ConfirmDialog('Are you sure?');
});
2
  • How about some real code instead of psuedo code -- please show us what you have tried, and indicate what is not working. Commented Mar 14, 2013 at 22:14
  • @MarkRajcok thats fair, I have added what I have. Any thoughts am I in the right direction? Commented Mar 14, 2013 at 22:24

5 Answers 5

43

Seems like an AngularJS directive is a bit over-the-top for a solution to this. Seems easier just to use straight javascript unless you need some custom functionality to your "confirm()" function.

if (confirm('Are you sure you want to delete this?')) {
     // TODO:  Do something here if the answer is "Ok".
}

Hope this helps, cheers

UPDATE: Actually, with Angular, it would be better to use $window.confirm as this would allow you to test with Karma/Jasmine.

Sign up to request clarification or add additional context in comments.

2 Comments

this would work fine until the user chooses not to display alerts/confirms in Chrome. In Chrome, when you get a confirm, there is a checkbox "do not show messages like this". And it's gone.
Sure, but the alerts is on by default. To prevent that, a directive that handles alert boxes without using confirm() would fix that.
28

Here's another approach at this. Basically it's a directive that gets the warning string you want to show, and the function to call if the user accepts. Usage example:

<button type="button" ng-really-message="Are you sure?"
ng-really-click="delete()">Delete</button>

5 Comments

A basic browser dialog box can be displayed without a directive if(confirim('Are you sure?')) { Do Something } does the exact same thing which much less overhead.
Please include all code for your solution in your answer.
He did include all the necessary code - click on the word 'Here'. And, no, you cannot use confirm with ng-click because you cannot have control flow inside those expressions.
@GuyArgo it is OK to link to external site, however; all relevant code should be included in the answer in case the site becomes unavailable See Provide Context for Links. And the control flow could be placed in the delete() method which removes the need for the directive Example
if this needs to be implemented in google extension you can't use inline javascript so directive will be a good solution.
9

This is how we're handling our 'confirmation dialogs' (using bootstrap)

The Markup

<div class="alert alert-block alert-error notification fade in" data-ng-show="displayLocationDeletePopup">
    <h6>Are you sure you want to delete this location?</h6>
    <div class="form-controls-alert">
        <a href="" class="btn" data-ng-click="showDeleteLocationPopup(false)">No</a>
        <a href="" class="btn btn-danger" data-ng-click="deleteVendorLocation(locationId)">Yes</a>
    </div>
</div><!-- end alert -->    

Setting model to false on controller load to hide by default with ng-show

$scope.displayLocationDeletePopup = false;

On click on event for show popup, calls a function/passes model in

<i class="icon-remove" data-ng-click="showDeleteLocationPopup(true, location)"></i>

In the controller:

$scope.showDeleteLocationPopup = function(options, id) {
    if (options === true) {
        $scope.displayLocationDeletePopup = true;
    } else {
        $scope.displayLocationDeletePopup = false;
    }
    $scope.locationId = id;
};

And per the anchors in the html above, can either close the popup or run the function

$scope.deleteVendorLocation = function (storeLocation) {
   // Code to run on confirmation            
};

1 Comment

I like this one for bootstrap users.
-1
var r = confirm("Are you sure you want to Permanently delete this order?");
if (r == true) {
    (OK button click) Write the function here.....
} else {
    (Cancle button click) Write the function here.....
}

Comments

-1

Place Delete option on the right hand side of each record and on clicking the delete option the record should get deleted from the details and JSON array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.