1

Here is my code where i am using sweetalert latest code for showing up a confirmation just like a javascript confirm box to act if ok, is pressed adn if its a cancel, do nothing,

Here is my try and my original code

var rtrnval = swal({   
                    title: "Check!",   
                    text: "You must select a task before entering hours.",  
                    type: "warning", 
                    showCancelButton: true,   
                    confirmButtonColor: "##DD6B55",   
                    confirmButtonText: "Confirm",   
                    closeOnConfirm: false 
                }, function(isConfirm){   
                    return isConfirm; //Will be either true or false
                }); 

                alert(rtrnval); return;

my original code is like this

var rtrnval  = confirm("You must select a task before entering hours.");

it is not showing the sweetalert box when i change confirm to swal, it throws error on the function but when i use confirm, the function works properly,

i am unable to read the value being returned to me like true/false so i ca act upon it

any guidance, greatly appreciated

i already posted code what i tried

here is what i want to replace

function ConfirmDelete(id){
                        
            var rtrnval  = confirm("are you sure!!");
            if (rtrnval == 1)
            {
                location='page2.php='+id
            }
            return 0
    
        }

and some cases i am doing this

return  confirm("are you sure")
jfir
  • 11
  • 2
  • 1
    The reason why you need to provide a callback function (the function with the `isConfirm` parameter) to Sweet Alert, is because the code doesn't wait for someone to click the button (unlike `confirm` which blocks the thread). Call `alert()` inside of the callback function. – Ivar Dec 29 '22 at 18:50
  • so what does it mean, should it be working with confirm or not – jfir Dec 29 '22 at 19:28
  • I see your question have been marked as a duplicate of a question that seems to explain it properly. Usually you want to avoid `confirm` and similar modals _because_ it blocks the thread. It's not a good user experience to have the page freeze as long as you haven't clicked any button which is what happens there. You'll have to live with the asynchronous nature of JavaScript (which applies to many aspects of the language). Like I said, in order to have an `alert()` when a choice is made, you can move the call to `alert()` inside of that callback function. – Ivar Dec 29 '22 at 20:28
  • We don't know what exactly you are trying to do, so we can't really give you more specific pointers. – Ivar Dec 29 '22 at 20:28
  • i am trying just to replace with confirm, here is an example as i have updated in my code – jfir Dec 30 '22 at 13:17
  • You can't blindly replace `confirm` with something that works asynchronous. How you implement it depends on the use case. For the first one you could easily place the if-statement inside of the callback function for it to work. – Ivar Dec 30 '22 at 14:18

1 Answers1

0

Try this:

Swal.fire({
  title: "Check!",   
  text: "You must select a task before entering hours.",  
  type: "warning", 
  showCancelButton: true,   
  confirmButtonColor: "##DD6B55",   
  confirmButtonText: "Confirm",   
  closeOnConfirm: false
}).then((result) => {
  if (result.isConfirmed) {
    console.log('Confirmed');
  } else if (result.isDenied) {
    console.log('Canceled');
  }
})
NeNaD
  • 18,172
  • 8
  • 47
  • 89