1

Hi I am new in using sweet alert js to make my alert box more fancy. I am using the normal javascript alert confirmation to delete a specific data in my table. However when I try to run a sweet alert confirmation before deleting it deletes the file without the confirmation popping up.

Here is the code in my JS below.

$(".delete-alert").click(function(event) {
  event.preventDefault();
  swal({
  title: 'Are you sure?',
  text: "You won't be able to revert this!",
  type: 'warning',
  showCancelButton: true,
  confirmButtonColor: '#3085d6',
  cancelButtonColor: '#d33',
  confirmButtonText: 'Yes, delete it!'
}).then(function () {
  swal(
    'Deleted!',
    'Your file has been deleted.',
    'success'
  )
});
});

this is my ruby on rails with HTML which calls the above js when clicking on trash icon

<%= link_to raw('<i class="fa fa-trash delete-alert"></i>'), candidate_path(f.id),method: :delete %>
Hacker621
  • 55
  • 4
  • first, you need to add in gem file how to do that please refer to this link [https://github.com/mois3x/sweet-alert-rails-confirm] – Anand Choudhary Sep 19 '17 at 06:37

1 Answers1

0

Here is the perfect example how you should use Sweet alert.

            swal({
                title: "Are you sure?",
                text: "You will not be able to recover this imaginary file!",
                type: "warning",
                showCancelButton: true,
                confirmButtonClass: 'btn-danger',
                confirmButtonText: 'Yes, delete it!',
                cancelButtonText: "No, cancel plx!",
                closeOnConfirm: false,
                closeOnCancel: false
            },
            function (isConfirm) {
                if (isConfirm) {
                    // Call your delete item function here.
                    swal("Deleted!", "Your imaginary file has been ted!", "success");
                } else {
                swal("Cancelled", "Your imaginary file is safe :)", "error");
                }
            });
Himanshu Upadhyay
  • 6,558
  • 1
  • 20
  • 33
  • I was asking how to make the item delete only when i confirm it on sweetalert . As of now , the item is getting deleted when i click on the trash icon ( used inside ruby tag ) without getting a confirmation from the sweetalert ( sweetalert box appears but it doesn't wait for my deletion confirmation ). My exact question was asked and solved in this link https://stackoverflow.com/questions/40579520/delete-method-with-sweet-alert-in-laravel but its in laravel framework . – Hacker621 Sep 19 '17 at 06:53
  • You have not posted the code which deletes the item. But what I can suggest you is, you call that delete item function inside `if (isConfirm) {}` so it will be called only if user clicks OK on sweet alert and if user clicks Cancel, the delete item function will not be called. – Himanshu Upadhyay Sep 19 '17 at 06:55