Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add HTML input into sweet alert?

I am trying to add radio button into dialog box using sweet alert but I'm not able to do it. Following is the code:

swal({
        title: "<small>Please select an reason to cancel this job !</small>",
        type: "warning",
        text:"<input type=\"radio\" name=\"reason\" value=\"male\">Reason 1<br><input type=\"radio\" name=\"reason\" value=\"female\">Reason 2<br><input type=\"radio\" name=\"reason\" value=\"female\">Other Reason",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "Yes",
        cancelButtonText: "No",
        closeOnConfirm: false,
        closeOnCancel: false,
        html: true
    },
            function(isConfirm){
                if (isConfirm) {
                    swal("Result !","Job cancelled successfully.");
                } else {
                    swal("Cancelled  !", "Process aborted");
                }
            });
like image 892
b22 Avatar asked Aug 14 '15 14:08

b22


1 Answers

SweetAlert2 supports radio inputs out of the box: https://sweetalert2.github.io/#input-radio

Swal.fire({
  title: 'Select color',
  input: 'radio',
  inputOptions: {
    '#ff0000': 'Red',
    '#00ff00': 'Green',
    '#0000ff': 'Blue'
  },

  // validator is optional
  inputValidator: function(result) {
    if (!result) {
      return 'You need to select something!';
    }
  }
}).then(function(result) {
  if (result.isConfirmed) {
    Swal.fire({
      icon: 'success',
      html: 'You selected: ' + result.value
    });
  }
})
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
like image 99
Limon Monte Avatar answered Oct 18 '22 18:10

Limon Monte