Sam Skirrow
Sam Skirrow

Reputation: 3697

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

Upvotes: 0

Views: 112

Answers (1)

Dhara Parmar
Dhara Parmar

Reputation: 8101

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; 
           });

Upvotes: 2

Related Questions