I want to do something very simple. I have one button in my page.
If you are using ajax then (making it as simple as possible)
Add your loading gif image to html and make it hidden (using style in html itself now, you can add it to separate CSS):
<img src="path\to\loading\gif" id="img" style="display:none"/ >
Show the image when button is clicked and hide it again on success function
$('#buttonID').click(function(){
$('#img').show(); //<----here
$.ajax({
....
success:function(result){
$('#img').hide(); //<--- hide again
}
}
Make sure you hide the image on ajax error callbacks too to make sure the gif hides even if the ajax fails.
$("#btnId").click(function(e){
e.preventDefault();
$.ajax({
...
beforeSend : function(xhr, opts){
//show loading gif
},
success: function(){
},
complete : function() {
//remove loading gif
}
});
});
The best loading and blocking that particular div for ajax call until it succeeded is Blockui
go through this link http://www.malsup.com/jquery/block/#element
example usage:
<span class="no-display smallLoader"><img src="/images/loader-small.png" /></span>
script
jQuery.ajax(
{
url: site_path+"/restaurantlist/addtocart",
type: "POST",
success: function (data) {
jQuery("#id").unblock();
},
beforeSend:function (data){
jQuery("#id").block({
message: jQuery(".smallLoader").html(),
css: {
border: 'none',
backgroundColor: 'none'
},
overlayCSS: { backgroundColor: '#afafaf' }
});
}
});
hope this helps really it is very interactive.
//do processing
$(this).attr("label", $(this).text()).text("loading ....").animate({ disabled: true }, 1000, function () {
//original event call
$.when($(elm).delay(1000).one("click")).done(function () {//processing finalized
$(this).text($(this).attr("label")).animate({ disabled: false }, 1000, function () {
})
});
});
I know this a very much late reply but I saw this query recently And found a working scenario,
OnClick of Submit use the below code:
$('#Submit').click(function ()
{ $(this).html('<img src="icon-loading.gif" />'); // A GIF Image of Your choice
return false });
To Stop the Gif use the below code:
$('#Submit').ajaxStop();
try
$("#btnId").click(function(e){
e.preventDefault();
//show loading gif
$.ajax({
...
success:function(data){
//remove gif
},
error:function(){//remove gif}
});
});
EDIT: after reading the comments
in case you decide against ajax
$("#btnId").click(function(e){
e.preventDefault();
//show loading gif
$(this).closest('form').submit();
});