Sam Skirrow Sam Skirrow - 6 months ago 11
Javascript Question

Add Class to form while it is being submitted via AJAX

I have a basic form which I am submitting via AJAX. I want to add a class to my form whilst the AJAX request is being sent so that I can style it slightly differently.

Is there a way to hook into AJAX whilst submitting and use jQuery addClass? Here is my AJAX script:

jQuery(document).ready(function() {
jQuery('#pay_what_you_want_form').submit(function() {
jQuery(this).ajaxSubmit({
success: function(){
alert('Settings have been updated');

},
timeout: 5000
});
return false;
});
});


I would like to add the class "loading" to the form with ID #pay_what_you_want_form

Answer

Yes can add class while submitting:

           jQuery('#pay_what_you_want_form').submit(function() {
              jQuery('#pay_what_you_want_form').addClass("loading"); // add class here 
              jQuery(this).ajaxSubmit({
                 success: function(){
                     alert('Settings have been updated');
                     jQuery('#pay_what_you_want_form').removeClass("loading");  // Remove class here as form is submitted now  
                 }, 
                 timeout: 5000
              }); 
              return false; 
           });
Comments