2

I need a little help. I'm struggling with some problem for a while. I need to call jquery load function every time i click on a button. To trigger load function I suppose I need to refresh that particular div or load that div again.

My goal is:

when I click on the button I want loading-div to be shown and I want .image-container to load again. I suppose that should trigger jquery load function that will hide loading-div when .image-container loads.

This is my code:

$(".image-container").load(function(){
   $( "#loading-div" ).hide();
});

$("#button").on('click', function(){
   $( "#loading-div" ).show();
   $(".image-container").load("index.php"); // load again div that sholud trigger upper function
});
1
  • since it was already loaded .. It may need a callback?? Commented Jan 3, 2016 at 2:23

3 Answers 3

3

Try this

$("#button").on('click', function(){
    $( "#loading-div" ).show();
    $(".image-container").load("index.php", function () {
        $( "#loading-div" ).hide();
    });
});
Sign up to request clarification or add additional context in comments.

1 Comment

it only shows div but it doesnt hide it. I have put alert above hide function and it doesnt call it
0

suppose I want to Load data from the server to the particular div tag continuously. forthat i use setinterval jquery method. example:

setinterval(function(){
$("div1").load("url","data you want to send to your  server");                      
                      },1000); 

note: load method work only for 'get' method.in above code second parameter of load is optional.

Comments

0

Mmm, just separate desired pre/post-load logic from handlers?

function loadSome(el, data) {
   $("#loading-div").show();
   el.load(data, function () {
     $("#loading-div").hide();
   });
}

function loadImage() {
    loadSome($(".image-container"), "index.php");
}

loadImage();
$("#button").on('click', function(){
    loadImage();
});

Or in more jQuery-way:

jQuery.fn.progressedLoad = function (data, progressElement) {
    $(progressElement).show();
    // add more complex load-progress logic here...

    return this.load(data, function() {
        $(progressElement).hide();
    });
}

...

$(".image-container").progressedLoad(
    {"url": "index.php"}, 
    $("#loading-div")
);

4 Comments

i get the same result as in above answer, it only shows but it doesn hide loading-div
1) is desired content loaded into .image-container, or it just shows #loading-div and then do nothing? 2) which version of jQuery you using? .load() can have different parameters list and can behave like .on("load", function(){}) in some usecases in jQuery versions prior to 1.9
1) every time i click on button content of .image-container changes, but sometimes it last few seconds, so i created function that will show #loading-div until loads finish 2) jquery-2.1.3
Its odd, as it works fine for me. Well... so: 1) whats in console? any errors on load? 2) is #loading-div inside .image-container or maybe its changing in some other way, so $("#loading-div") reference becomes invalid? have you tried $(".image-container").progressedLoad({"url": "index.php"}, "#loading-div"); instead? 3) is there possibly param-list difference? have you tried $(".image-container").progressedLoad("index.php", "#loading-div"); as well? 4) have you tried to create minimal reproducible example on something like jsFiddle?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.