10

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,653
  • 2
  • 28
  • 58

3 Answers3

20
$('#info').fadeOut(500, function() {
   $(this).empty().show();
});
Hadi Mostafapour
  • 1,513
  • 1
  • 9
  • 20
  • 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
5
$('#info').fadeOut(500, function() {
   $(this).html('').show();
});

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

adeneo
  • 293,187
  • 26
  • 361
  • 361
2

Use fadeOut's callback:

$('#info').fadeOut(500, function() {
  $('#info').html("New Content").show();
});
Erik Petersen
  • 2,277
  • 11
  • 16