[Answer]-Delete happening on cancel button click

0👍

The condition in one event handler doesn’t necessarily work in the second event handler, you have to join it all in one event handler :

$('.type_delete').on('click', function (e) {
    e.preventDefault();
    if (confirm("Are you sure want to delete record?")) {
        var csrf_token = $("#csrf_token").val(),
            id         = this.id;
        $.ajax({
            data: {
                csrfmiddlewaretoken: '{{'+csrf_token+'}}',
                delete: id
            },
            type: 'POST',
            url: '/setting/type/',
            cache: false
        });
    }
});
👤adeneo

1👍

By the time your confirmation box is displayed, the call to second function is already underway. You need to define the second function as another independent function and then call it from within first one.

Here is an example:

 $('.delete').click(function (e) {

   e.preventDefault();
   if (confirm("Are you sure want to delete record?")) {
       doDelete($(this).attr('id'))
   }
 });




 function doDelete(elemId) {
    var csrf_token = $("#csrf_token").val();
    var id = elemId;
    $.ajax({ // create an AJAX call...
        data:{
            csrfmiddlewaretoken: ('{{csrf_token}}'),
            delete:id
        },
        type:'POST',
        url: '/setting/type/', // the file to call
        cache:false,
        success: function() { // on success..
            window.location.href = window.location;
        });
    }

Leave a comment