title: how to auto scroll down in div on content added?
body: <p>I am developing a chat program using jquery, php and ajax. and my problem is that when user puts msgs and when the chat box is overflowed it should auto scroll down when new msg enter and chat box overflowed. What I tried to achieve is this.</p>
  
  <p><strong>CODE</strong></p>
  
  <pre><code> $.ajaxSetup ({
    cache: false
   });
  $(document).ready(function(e) {
  
  
  setInterval (function() {
      $('.main').load('display_messages.php');
  },1000)
  
  $(function() {
      //Function for press Return button
      $('#newMessageContent').keypress(function(e) {
          if (event.keyCode == 13 || !event.keyCode == all  ) {
                  setTimeout (function() {
                      $('.main').load('display_messages.php');
                      setTimeout (function() {
                          $('.main p:last').addClass('animated fadeIn');
                      },20)
                  },50);
              //$('.main').stop().animate({scrollTop:$('.main')[0].scrollHeight}, 1000);
          }
      });
      // Functions for click button
      $('#newMessageSend').click(function() {
          setTimeout (function() {
                  $('.main').load('display_messages.php');
                  setTimeout (function() {
                      $('.main p:last').addClass('animated fadeIn');
                  },20)
          },50);
          //$('.main').stop().animate({scrollTop:$('.main')[0].scrollHeight}, 1000);
      });
  
  
  
  
      var chckIt = setInterval(function(){
           $('.main').one('DOMSubtreeModified',function(){
               setTimeout(function() {
                  $('.main').stop().animate({scrollTop:$('.main')[0].scrollHeight}, 1000);
               },500);
           })
      },1000)
  
      $('.main').on('scroll',function(){
          if ( $('.main').scrollTop() === 0 ){
          clearInterval(chckIt);
          }
      });
  
   });    
  
  });
  </code></pre>
  
  <p>it has some bugs. it will auto scroll down but it repeat itself again and again and not stoping, and you cant scroll up to see older msgs if u want.</p>
  
  <p>so then I used this function <code>$('.main').on('scroll',function(){
              if ( $('.main').scrollTop() === 0 ){
              clearInterval(chckIt);
              }
          });</code> to control it but its useless.</p>
  
  <p>here is html.</p>
  
  <p><strong>code</strong></p>
  
  <pre><code>        &lt;div class="chatBox"&gt;
      &lt;div class="chatlogo"&gt;
              &lt;embed src="test2.swf" play="true" class="vid2" loop="true" width="180" height="50" salign="C" scale="ShowAll" id="vid_2" quality="High" wmode="transparent" name="vid_2" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"&gt;
        &lt;/div&gt;
          &lt;div class="user"&gt;
  
          &lt;form name="signIn" id="signInForm" action="signout_delete_content.php" method="post" onSubmit="return false"&gt;
              &lt;span class="error animated"&gt;Invalid uername&lt;/span&gt;
              &lt;input name="username" type="text" id="username" placeholder="Enter username" size="13px" onClick='document.username.value = "" '&gt;
              &lt;input type="submit" id="signIn" value="SING IN"&gt;
          &lt;/form&gt;
              &lt;span class="welcome"&gt;&lt;/span&gt;
              &lt;input type="button" id="signOut" value="SIGN OUT" onclick="window.location.reload();"&gt;
          &lt;/div&gt;
          &lt;div class="main"&gt;
              &lt;p&gt;Admistrator: Hello msgs here.&lt;/p&gt;
          &lt;/div&gt;
          &lt;div class="messageBox"&gt;
              &lt;form name="messageBoxSignInForm" id="messageBoxSignInForm" onSubmit="return false"&gt;
              &lt;input type="submit" id="messageBoxSignIn" value="Sign In to Enter Chat"&gt;
              &lt;/form&gt;
            &lt;form name="newMessage" class="newMessage" action="" onSubmit="return false"&gt;
              &lt;textarea name="newMessageContent" id="newMessageContent" placeholder="Enter your message here."&gt;Enter your message here&lt;/textarea&gt;
              &lt;input type="submit" id="newMessageSend" value="Send"&gt;
  
            &lt;/form&gt;
        &lt;/div&gt;
      &lt;/div&gt;
  </code></pre>
  
  <p>any help would be appreciated. thanks</p>