0

I have this code, that executes when a link is clicked:

$.get(file.html, function(data) {           
  $('selector_1').html($(data).find('selector_2').html());
});

I would like to modify the code in such a way that when I click a link, the $.get command runs while selector_1 fades out, and when the HTML of selector_1 is placed, it fades back in.

How can this be done?

1 Answer 1

2

Assuming selector_1 and selector_2 are just a sample, try the below code which uses the fadeOut callback.

$('selector_1').fadeOut(300, function() {        
    $.get(file.html, function(data) {
        $('selector_1')
            .html($(data).find('selector_2').html())
            .fadeIn();
    });    
});

Also note that the $.get is wrapped inside the fadeOut callback, so that the transition is perfect.

This is also the solution I thought of, but the problem is that the $.get command is sent after the fade out and not simultaneously - I want the $.get command to execute immediately.

See below.

$('selector_1').fadeOut(300);
$.get(file.html, function(data) {
    $('selector_1')
      .html($(data).find('selector_2').html())
      .fadeIn();
});
Sign up to request clarification or add additional context in comments.

5 Comments

> This is also the solution I thought of, but the problem is that the $.get command is sent after the fade out and not simultaneously - I want the $.get command to execute immediately.
@afcdesign Then just move it outside fadeOut
if I do that, the html placement can be seen while the fadeout happens. That is what I want to avoid.
The time taken by $.get is not constant. If it is too fast, then you will see the html placement.. to avoid that you should move that inside callback.. but as you know.. it would simply be triggered only after fadeOut.
can't I just get the $.get object as a variable that I can use? Then it is triggered and stored in cache, for me to use when I want.

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.