Spinner Doesn't Work While AJAX Does Its Job
I'm trying to get a spinner to show up while my form does submitting data. So far, my AJAX works absolutely fine and does its job; but the spinner just won't show up! Here's my JavaScript (properly queued inside WordPress) -
--
- jQuery(document).ready(function($){
- //'use strict';
- $("form").submit(function() {
- event.preventDefault();
- $(".myspinner").css('display', 'inline-block');
- var wpajaxurl = document.location.protocol + '//' + document.location.host + '/wp-admin/admin-ajax.php';
- var form = $(this);
- var io_form_data = form.serialize();
- alert("Did The Spinner Load?");
- $.ajax({
- type:'POST',
- dataType:'json',
- url:wpajaxurl,
- data:io_form_data,
- async:false,
- success:function (data, textStatus) {
- console.log('SUCCESS', data);
- alert("SUCCESS");
- },
- error:function (jqXHR, textStatus, errorThrown) {
- alert("FAIL");
- }
- });
- });
- });
--
Observations:
1. The spinner does show up when I use it inside AJAX, using 'beforeSend' -
--
$.ajax({
type:'POST',
dataType:'json',
url:wpajaxurl,
data:io_form_data,
async:false,
beforeSend: function() {
$(".myspinner").css('display', 'inline-block');
},
success:function (data, textStatus) {
console.log('SUCCESS', data);
alert("SUCCESS");
},
error:function (jqXHR, textStatus, errorThrown) {
alert("FAIL");
}
});
--
2. However, it won't show up if I put the selector before the ajax call.
My aim is to show the spinner as soon as the user clicks on the button and then goes off once AJAX is done. So far, the script works well and shows me 'SUCCESS' message as intended; but without the spinner.
I'd really appreciate if you could tell me what exactly is going wrong; and what is the proper way to debug such problems? Thank you for reading.