jQuery toggleClass example

Toggle class name on click in jQuery

by Sascha

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<div id="loading">

</div>

JavaScript

function ShowDialog(text) {
  $('#loading').html(text).dialog();
}

ShowDialog("Saving..."); // ... $("#loading").dialog("open);
var ajaxCall = $.ajax({
  url: "https://gist.githubusercontent.com/Moonbird-IT/da4c7d76a69eb250478bb55b5d2360f5/raw/9dbf9d92981a3c9b71906dd3a680a2cdeca7c4aa/googlecharts.json",
  dataType: 'json'
});

$.when(ajaxCall)
  .done(function() {
    console.log(1);
    $("#loading").dialog("close");
  })
  .then(function(data) {
    console.log(2);
    alert("Changes saved");
    console.log(3);
    ShowDialog("Redirecting...");
    console.log(4);
    // window.location = "some other location";
  })
  .fail(function(data) {
    // some code here
  });