1

I want Sweet alert to work first to change the check to true, false, what to do? Please help me !

$("#our-table").on('click', '#button-delete', function () {
    var check = null;

    Swal.fire({
        title: 'Are you sure ?',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Comfirm'
    }).then((result) => {
        if (result.isConfirmed) {
            check == true
        } 
    })

    if (check == true) {
        return true;
    }
    else {
        return false;
    }
});
Tee Adkins
  • 43
  • 4
  • What's your goal? You will make your check to true or false then show something(or return something) when sweetalert's confirm is clicked? – Shahnawaz Hossan Dec 19 '20 at 08:49

2 Answers2

0

Think the only thing you need to change is

 if (result.isConfirmed) {
     check = true
   } 

The == is actually doing a check instead of an assignment.

0

Firstly if you want to return true or false according to sweetalert's confirmation, you can pass a callback function and return the checked value in this way:

function sweetChecked(callback) {
    Swal.fire({
        title: 'Are you sure ?',
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Comfirm'
    }).then((result) => {
        callback(result.isConfirmed);
    })
}

$("#button-delete").on('click', function (event) {
    event.preventDefault();
    var URLToRedirect = event.currentTarget.getAttribute('href');
    console.log(URLToRedirect);
    sweetChecked(function (checked) {
        if (checked) {
            console.log("true");
            window.location.href = URLToRedirect;
        }
        else {
            console.log("false");
        }
    });
});
<script src="https://code.jquery.com/jquery-3.5.1.min.js"
    integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0=" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@10.12.5/dist/sweetalert2.all.min.js"></script>

<a id="button-delete" href="/ManageUser/Delete/01">Delete</a>
Shahnawaz Hossan
  • 2,695
  • 2
  • 13
  • 24