Fadeout + empty a div, and then put new content in
Asked Answered
G

3

11

What is a good way to fadeout the content of a div, but keeping the div ready for new content?

With

$('#info').html('').fadeOut(500);
or
$('#info').fadeOut(500).html('').show();

The div content just disappears, and new content does not show

With

 $('#info').fadeOut(500);

The div fades as it should, but any new content does not show

Galvanize answered 10/5, 2012 at 23:14 Comment(0)
D
22
$('#info').fadeOut(500, function() {
   $(this).empty().show();
});
Doiron answered 10/5, 2012 at 23:23 Comment(3)
+1 for using empty() instead of html('') since that's what it's there for.Zetes
What would be the method for setting content to a div and fading it in then? EDIT: Ah got it: $('#info').hide().html('new content').fadeIn(200); Is that the best way?Galvanize
Yes, you don't need a callback when fading in!Conjunctiva
C
6
$('#info').fadeOut(500, function() {
   $(this).html('').show();
});

Will wait until the div is faded out before emtying it!

Conjunctiva answered 10/5, 2012 at 23:17 Comment(0)
E
2

Use fadeOut's callback:

$('#info').fadeOut(500, function() {
  $('#info').html("New Content").show();
});
Educative answered 10/5, 2012 at 23:16 Comment(1)
You can use $(this) instead of finding the element again. :)Synthetic

© 2022 - 2024 — McMap. All rights reserved.