I'm looking to display a <div>
upon clicking submit before triggering $.ajax()
Here's my HTML:
<div id="waiting_div"></div>
This is the CSS:
#waiting_div {
position: fixed;
top: 0px;
left: 0px;
height: 100%;
width: 100%;
background-color: rgba(0, 0, 0, .8);
z-index: 999;
display: block;
}
These are the JavaScript functions:
jQuery(document).ready(function() {
jQuery("#waiting_div").hide();
});
function set_response_waiting() {
jQuery("#waiting_div").show();
}
function del_response_waiting() {
jQuery("#waiting_div").hide();
}
Here's the main JavaScript code:
jQuery("#save_changed_prices").click(function(){
set_response_waiting(); <-- the div is displayed here
var len = window.prices.length; //array with data for sending
var i = 0;
for (i = 0; i < len; i++) {
if (window.prices[i].price >= 0) {
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {...... },
async: false
}).done(function (data) {
...
}).fail(function () {
...
}).always(function () {
...
});
}
}
del_response_waiting(); <-- the div is hidden here
});
However, the set_response_waiting() function doesn't display my "#waiting_div" before sending.
I need to refresh or update the DOM tree before sending. Any suggestions on how to achieve this?
The following approach also doesn't work:
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {
'action': 'update_price',
'car_id': car_id,
'dep_city_id': window.prices[i].dep,
'arr_city_id': window.prices[i].arr,
'price': window.prices[i].price
},
beforeSend: set_response_waiting(),
async: false
})