For each checkbox checked, I need to execute an AJAX request which is being handled inside of an each loop. When all AJAX requests are complete, I need to execute a follow-up function.
The number of checkboxes is always variable and I only need to run the AJAX if the box is checked.
The HTML:
<input type="checkbox" class="checkbox" val="1" />
<input type="checkbox" class="checkbox" val="2" />
<input type="checkbox" class="checkbox" val="3" />
<input type="checkbox" class="checkbox" val="4" />
<input type="checkbox" class="checkbox" val="5" />
<button id="btn1">Click Me</button>
jQuery (v2.1.4)
$(document).on('click', '#btn1', function() {
$('.checkbox').each(function() {
// We only want the checked boxes
if ($(this).is(':checked')) {
// Do the thing
$.get('test.html', function(data) {
console.log('Response received: ' + $(this).val());
});
}
});
console.log('All responses received');
});
What can I use to delay the final callback ("All responses received", in this example) until ALL of the AJAX requests are complete?
JSFiddle: https://jsfiddle.net/sjkhh43x/