sach jot
sach jot

Reputation: 570

jQuery Sweet Alert Unexpected 2nd argument error

I am using sweet alert plugin and getting an error. I have tried every example but can't understand what this error means

Uncaught SweetAlert: Unexpected 2nd argument (function() { setTimeout(function() {ckquote

My code:

<script type="text/javascript">
$('.delete-confirm').on('click', function() {
    var postID = $(this).val();
    console.log(postID);
    swal({
        title: "Are you sure?",
        text: "If you delete this post all associated comments also deleted permanently.",
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: false,
        showLoaderOnConfirm: true,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "Yes, delete it!",
    }, function() {
        setTimeout(function() {
            $.post("../delete.php", {
                    id: postID
                },
                function(data) {
                    swal({
                            title: "Deleted!",
                            text: "Your post has been deleted.",
                            type: "success"
                        },
                    );
                }
            );

        }, 50);
    });
});
</script>

My whole error on console window:

sweetalert.min.js:1 Uncaught SweetAlert: Unexpected 2nd argument (function() {
 setTimeout(function() {
 $.post("../delete.php", {
 id: postID
 },
 function(data) {
 swal({
 title: "Deleted!",
 text: "Your post has been deleted.",
 type: "success"
 },
 );
 }
 );

 }, 50);
 })

Upvotes: 3

Views: 8524

Answers (1)

Barmar
Barmar

Reputation: 782785

Sweet Alert can be invoked in two ways

  • 1, 2, or 3 strings parameters

    swal(["title",] "text" [, "iconname"])
    
  • a single object parameter containing all the options:

    swal({
        title: "Are you sure?",
        text: "If you delete this post all associated comments also deleted permanently.",
        type: "warning",
        showCancelButton: true,
        closeOnConfirm: false,
        showLoaderOnConfirm: true,
        confirmButtonClass: "btn-danger",
        confirmButtonText: "Yes, delete it!",
    });
    

If you want to do something with the response, it returns a promise, and you can retrieve the value with .then:

swal({
  title: "Are you sure?",
  text: "If you delete this post all associated comments also deleted permanently.",
  type: "warning",
  showCancelButton: true,
  closeOnConfirm: false,
  showLoaderOnConfirm: true,
  confirmButtonClass: "btn-danger",
  confirmButtonText: "Yes, delete it!",
}).then(function() {
  setTimeout(function() {
    $.post("../delete.php", {
        id: postID
      },
      function(data) {
        swal({
          title: "Deleted!",
          text: "Your post has been deleted.",
          type: "success"
        }, );
      }
    );

  }, 50);
});

Upvotes: 4

Related Questions