Your problem is that you call removeClass('loading') right after you issue the ajax call, and not after it ends.
You can do removeClass('loading') in ajax callback. $.post only provides a success callback:
jQuery.post( url, [ data ], [ success(data, textStatus, XMLHttpRequest) ], [ dataType ] )
And the image loading should be deleted in any case - successful or unsuccessful, therefore it is better to use the complete $.ajax() :
$('#searchform').submit(function(){ $('#searchresults').addClass('loading'); $.ajax({ type: 'POST', url: 'search.php', data: $('#'+this.id).serialize(), success: function(result){ $('#searchresults').html(result); }, complete: function() { $('#searchresults').removeClass('loading'); } }); return false; });
For more information about calls, see the Callback Functions section of $. ajax () docs
source share