I am using bootstrap and I am trying to setup a spinner when a form is submitted. I am able to get the spinner button to spin when the form is submitted however after it is submitted it just keeps spinning. I have looked all over but can't seem to figure out how to get this to stop once the API call is complete. How can i get this to spin when form is submitted, stop once API call is done and return back to usable button again after API call is complete?
html Code
<form id="form_id" name="form_id" class="form_id" method="POST" >
<div class="input-group-append">
<button type="submit" id="btnFetch" class="btn btn-primary mb-2">Submit</button>
</div>
</form>
JS Code
$(document).ready(function() {
$("#btnFetch").click(function() {
$(this).prop("disabled", true);
$(this).html(
`<span class="spinner-border spinner-border-sm" role="status" aria-hidden="true"></span>Loading...`
);
});
});
Ajax call
$(document).ready(function() {
$('.form_class1').on('submit', function(event) {
$.ajax({
data : {
some_id: $('#some_id').val(),
},
type : 'POST',
url : '/ajax_request_url1'
})
.done(function(data) {
if (data.error) {
}
else {
// Do Processing here....
}
});
event.preventDefault();
event.stopImmediatePropagation();
});
});