I'm trying to show and hide a spinner on a submit button of a contact form. While the form is being submitted the spinner should be showed on the button. As soon as the form gets submitted the spinner should be hidden.
Here's my submit button along with the spinner span:
<div class="d-flex justify-content-center">
<button class="btn btn-medium btn-fancy btn-primary mb-0 ls-wider" type="submit" id="submitButton">
<span class="submitSpinner spinner-border spinner-border-sm me-1"></span> Send Message
</button>
</div>
And this is the AJAX function I'm using:
$(document).ready(function() {
$('.submitSpinner').css('display','none');
$("#contact-form").submit(function(e) {
e.preventDefault();
$('#submitButton').text('Sending Message');
$('.submitSpinner').css('display','inline-block');
var data = {
'name': $('#name').val(),
'email': $('#email').val(),
'phone': $('#phone').val(),
'company': $('#company').val(),
'message': $('#message').val(),
}
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type: "POST",
url: "/contact/message/send",
data: data,
dataType: "json",
success: function(response) {
// console.log(response);
if (response.status == 400) {
$.each(response.errors, function(key, error) {
$('#contact-alerts').append('<div class="col-12 col-md-9 col-lg-7 alert alert-danger alert-dismissible fade in show" role="alert"><button type="button" class="btn-close line-height-unset" data-bs-dismiss="alert" aria-label="Close"></button>' + error + '</div>')
});
$('.submitSpinner').css('display','none');
} else {
$("#contact-form")[0].reset();
$('#submitButton').text('Send Message');
$('.submitSpinner').css('display','none');
$.magnificPopup.open({
showCloseBtn: true,
items: {
src: '#contact-success'
},
type: 'inline',
fixedContentPos: false,
fixedBgPos: true,
overflowY: 'auto',
closeBtnInside: true,
preloader: false,
midClick: true,
removalDelay: 300,
blackbg: true,
mainClass: 'my-mfp-slide-bottom',
});
}
}
});
});
});
display
being set tonone
and can you see the spinner if you set thedisplay
to justinline-block
without setting it in your function here?.show()
and.hide()
instead of setting the CSS properties manually have you tried that? Also for showing a spinner I would usediv
since this is an element that should be displayed as a block whilespan
is justinline
by default.But through Javascript it doesn't.
-> why you are trying to mix javascript with above jquery code?