tags:

views:

29

answers:

2
+1  A: 

Try:

success: function(msg) {
  $('.ContentsMainRight').fadeOut(500, function() {
    $('.ContentsMainRight').html(msg.d);
    $('.ContentsMainRight').fadeIn(1000);
  });
},

Basically the problem is that you hide the children and then replace the children with your html() call. The replaced content isn't hidden so it's instantly visible, which is what you're seeing.

Another issue is that if there are multiple children you will be replacing all the children on each child's callback. When you call fadeOut() on multiple elements, it's called separately for each element.

To give you an example of what I mean assume:

<ul>
  <li>one</li>
  <li>two</li>
  <li>three</li>
</ul>

Compare:

$("ul").fadeOut(500, function() {
  alert("replacing");
  $(this).html("<li>four</li><li>five</li><li>six</li>").fadeIn(500);
});

with:

$("ul").children().fadeOut(500, function() {
  alert("replacing"); // this will be called three times
  $(this).html("<li>four</li><li>five</li><li>six</li>").children().fadeIn(500);
});

or to make it even more clear:

$("ul").children().fadeOut(500, function() {
  alert("replacing"); // this will be called three times
  $(this).append("<li>four</li><li>five</li><li>six</li>").children().fadeIn(500);
});
cletus
Hi Cletus. I only have one element with the ContentsMainRight class, so the replace of several elements in not an issue, but that the replaced content is visible is the problem. Thanks for letting me know the (obvious) reason.
Øyvind Bråthen
Your example is very good. I fixed this now putting another div inside my ContentsMainRight div, and then fading that div in and out without the call to children. I'm quite new to jQuery so I appreciate the help :)
Øyvind Bråthen
+2  A: 

Most likely, .children() references elements, not text nodes. This means the styles that are applied to them won't persist when the text changes, meaning the text will not have display:none or visibility:hidden (whichever is applied).

Why don't you just fade out the .ContentsMainRight div?

success: function(msg) {
  $('.ContentsMainRight').fadeOut(500, function() {
    $('.ContentsMainRight').html(msg.d);
    $('.ContentsMainRight').fadeIn(1000);
  });
},
Andy E
Hi Andy. It seems that the problem is as you say. Even if I remove the fadeIn call completly, the text i input still gets visible.The reason I don't fade out the entire div, is that it would mean that the background image for the div also get's faded out, and that is not what I want. Would it help if I put a second div inside the div with the background, and then fade that div out, and replace the contents inside that div instead of the actual elements inside the div?
Øyvind Bråthen
I tried putting a new div inside the ContentsMainRight div, and that worked like a charm. Thanks for your insight on this issue.
Øyvind Bråthen
@Daemon: glad you got it working :-)
Andy E