0

Currently I have a delete button on my datatable and I'm using data-id & class btnDelete just to get it the id foreach row.

<button data-id="1" class="btnDelete">Delete</button>

Now I have this jQuery & SweetAlert code

$(".btnDelete").click(function() {
    var id = $(this).attr('data-id');
    Swal.fire({
        title: 'Are you sure?',
        text: "You won't be able to revert this!",
        icon: 'warning',
        showCancelButton: true,
        confirmButtonColor: '#3085d6',
        cancelButtonColor: '#d33',
        confirmButtonText: 'Yes, delete it!'
    }).then((result) => {
        if (result.value) {
            alert(id);
        }
    });
});

But this gives me an error like this

Uncaught (in promise) ReferenceError: id is not defined

BTW: I can alert the var id outside of the sweetalert but can't alert it inside the code of sweetalert

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
Pablo
  • 1,357
  • 1
  • 11
  • 40
  • 1
    The code you've shown won't cause the error you've described. Please update your question with a [mcve] demonstrating the problem, ideally a **runnable** one using Stack Snippets (the `[<>]` toolbar button; [here's how to do one](https://meta.stackoverflow.com/questions/358992/)). – T.J. Crowder May 23 '20 at 13:52
  • see if this helps :https://stackoverflow.com/questions/32912459/promises-pass-additional-parameters-to-then-chain – MJ Khan May 23 '20 at 13:57
  • or this -> https://stackoverflow.com/questions/42464449/promises-how-to-pass-variable-into-then-function – MJ Khan May 23 '20 at 13:58
  • because id is declared outside of [.then] function, so it is not accessible inside the function. – MJ Khan May 23 '20 at 13:59

0 Answers0