Skip to content

Instantly share code, notes, and snippets.

@fujimura
Created July 5, 2010 02:26
Show Gist options
  • Save fujimura/463939 to your computer and use it in GitHub Desktop.
Save fujimura/463939 to your computer and use it in GitHub Desktop.
// Set passed background-image to form type=submit just before ajax request was submitted.
// reset with ajax:complete.
// options:
// loadingImage: background-image
// loadingText: text
jQuery.fn.swapBackgroundToLoading = function(options){
$(this).live('ajax:before', function(){
var submit = $(this).find('input[type=submit]')
var text = submit.val();
var backgroundImage = submit.css('background-image');
// set loading
submit.val(options.loadingText || '');
submit.css('background-image', 'url(' + loadingImagePath + ')');
$(this).one('ajax:complete', function(){
// reset
submit.css('background-image', backgroundImage);
submit.val(text);
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment