0

I want to create function which inside include swall then call that function with another job...

here is my code:

function confirmSwal(ket, callback){
  swal({
    title: ket,
    showCancelButton: true,
    cancelButtonText: 'Batal',
    confirmButtonClass: 'btn-success',
    confirmButtonText: 'Hapus',
    closeOnConfirm: true
  },
  function(){
    callback();
  });
}

$("#hapusBulk").click(function(){
  confirmSwal("Apakah Anda Yakin Hapus Data Terpilih?", function(){
    alert("Asd");
  });
});

but alert doesn't work.. please help..

1
  • 1
    function(){ callback(); } Instead of this you need only callback Commented May 21, 2018 at 3:38

3 Answers 3

2

It should be like this.

function confirmSwal(ket, callback){
    swal({
        title: ket,
        showCancelButton: true,
        cancelButtonText: 'Batal',
        confirmButtonClass: 'btn-success',
        confirmButtonText: 'Hapus',
        closeOnConfirm: true
    }, callback
    )}

$("#hapusBulk").click(function(){
    confirmSwal("Apakah Anda Yakin Hapus Data Terpilih?", function(){
        alert("Asd");
    });
});

because of your callback variable is defined as a function, and you don't require to have a function to wrap the callback value.

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

2 Comments

thanks bro.. it work but i want when click confirm button true then do callback..
im add code like .then(function(result){ if(result.value) { callback(); } }) and it work perfectly..
0

Use then method

Here is the jsbin link with working implementation: https://jsbin.com/tawesufuge/1/edit?html,js,output

function confirmSwal(ket, callback){
  swal({
    title: ket,
    showCancelButton: true,
    cancelButtonText: 'Batal',
    confirmButtonClass: 'btn-success',
    confirmButtonText: 'Hapus',
    closeOnConfirm: true
  }).then(callback);
}


$("#hapusBulk").click(function(){
  confirmSwal("Apakah Anda Yakin Hapus Data Terpilih?", function(){
    alert("Asd");
  });
});
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>
<script src="https://code.jquery.com/jquery-3.1.0.js"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

  <button id="hapusBulk">hapusBulk</button>
  
</body>
</html>

2 Comments

thanks bro.. it work but i want when click confirm button true then do callback..
im add code like .then(function(result){ if(result.value) { callback(); } }) and it work perfectly..
0

If the swal function accept a callback, you need to change your code to this :

function confirmSwal(ket, callback){
  swal({
    title: ket,
    showCancelButton: true,
    cancelButtonText: 'Batal',
    confirmButtonClass: 'btn-success',
    confirmButtonText: 'Hapus',
    closeOnConfirm: true
  }, callback() );
}

2 Comments

thanks bro.. it work but i want when click confirm button true then do callback..
im add code like .then(function(result){ if(result.value) { callback(); } }) and it work perfectly..

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.