Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to add checkbox in sweetAlert box?

Tags:

sweetalert

I can set inputType as password. What are the other input Types supported..?

       swal({
            title: "Are you sure?",             
            type: "input",
            inputType: "checkbox",             
            showCancelButton: true,
            closeOnConfirm: true,                
        }, function () {
            swal("", "You did it", "success");
        });

the checkbox inputType is not supported in swal..

like image 592
Sushil Chaskar Avatar asked Dec 23 '22 10:12

Sushil Chaskar


1 Answers

There's a nice way to use checkbox modal type in SweetAlert2:

Swal.fire({
  title: 'Do you have a bike?',
  input: 'checkbox',
  inputPlaceholder: 'I have a bike'
}).then((result) => {
  if (result.isConfirmed) {
    if (result.value) {
      Swal.fire({icon: 'success', text: 'You have a bike!'});
    } else {
      Swal.fire({icon: 'error', text: "You don't have a bike :("});
    }
  } else {
    console.log(`modal was dismissed by ${result.dismiss}`)
  }
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>

PS. notice that SweetAlert2 is a little bit different from SweetAlert, check the simple migration guide: https://github.com/sweetalert2/sweetalert2/wiki/Migration-from-SweetAlert-to-SweetAlert2

like image 85
Limon Monte Avatar answered Feb 15 '23 22:02

Limon Monte