JSFiddle - React, Tailwind, and code Playground

by frncsdrk

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/sweetalert/1.1.3/sweetalert.min.css">
<select id="dropdownId">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
</select>

JavaScript

var prev_val;
$('#' + "dropdownId").focus(function() {
  prev_val = $(this).val();
})
.change(function(e) {
	var select = this;
  $(this).blur();
  return swal({
      title: "Are you sure?",
      text: "Do you want to change the dropdown?",
      type: "warning",
      showCancelButton: true,
      confirmButtonColor: "#DD6B55",
      confirmButtonText: "Yes",
      cancelButtonText: "No",
      closeOnConfirm: true,
      closeOnCancel: true
    },
    function(isConfirm) {
      if (isConfirm) {
        e.preventDefault();
        return true;
      } else {
        $(select).val(prev_val);
        return false;
      }
    });
});