tags:

views:

32

answers:

4

So, I wrote a small page:

<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>  

    <script>  
       function slide() {  
          $("#d").slideUp("slow");  
          document.getElementById("d").innerHTML=Math.random();  
          $("#d").slideDown("slow");  
       }   
    </script>  

  </head>  
  <body>  

    <div id="d"></div>  
    <button onClick="slide()">do</button>  

  </body>  
</html>

And I don't know why but the text inside div is changed before the $("#d").slideDown("slow"), right when the slideUp begins.
I want it to work so that first #d slideUp, then while its height is close to 0 or something the innerHTML will change, and only after that slideDown.

A: 

First of all since you are using jQuery try changing the contents of #d using jQuery. Also, if you need the content change to happen when slideUp finishes, you need to do that inside a callback function inside slideUp.

cherouvim
+2  A: 

Do it this way:

function slide() {  
  $("#d").slideUp("slow", function() {
     $(this).html(Math.random).slideDown("slow");  
  });       
}

Here, the code inside the callback function gets called after the animation is completed. In your original code, this is not the case. The animation gets started and immediately after that the content gets changed.

Read the documentation about .slideUp()

Update:

Btw. document.getElementById is the same as $('#id') in jQuery. You can use the .html() function. No need to look for the same element twice. And the element being animated is available in the callback function via this.

Felix Kling
thanks, that worked perfectly for me
alterionisto
A: 

Two ways...

or using Queue or Slide Callback function

http://jsbin.com/otise/edit

balexandre
A: 

Since you are using jQuery, why don't you really use it?

$('#btnid').click(function() {
  $("#d").slideUp("slow");  
          $('#d').html(Math.random());  
          $("#d").slideDown("slow"); 
});

Just some improvement to your code

Omar Abid
It does not solve the OP's problem.
Felix Kling
You did solve his problem, no? So why repeating what you have already done. I'm just enhancing his code by taking full advantages of the jQuery functionalities.
Omar Abid