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

mowgli
  • 2,796
  • 3
  • 31
  • 68

3 Answers3

22
$('#info').fadeOut(500, function() {
   $(this).empty().show();
});
Hadi Mostafapour
  • 1,994
  • 2
  • 13
  • 21
  • 7
    +1 for using `empty()` instead of `html('')` since that's what it's there for. – Madbreaks May 10 '12 at 23:25
  • 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? – mowgli May 10 '12 at 23:35
  • Yes, you don't need a callback when fading in! – adeneo May 10 '12 at 23:47
6
$('#info').fadeOut(500, function() {
   $(this).html('').show();
});

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

adeneo
  • 312,895
  • 29
  • 395
  • 388
2

Use fadeOut's callback:

$('#info').fadeOut(500, function() {
  $('#info').html("New Content").show();
});
Erik Petersen
  • 2,347
  • 14
  • 17