Click here to Skip to main content
15,908,015 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
Hi I want to scroll div to bottom onload. I tried something like this, but it doesn't work.
function scrollToBottom() {


        var objDiv = document.getElementById("message");
        objDiv.scrollTop = objDiv.scrollHeight;
    }



This is code of div.
<div id="message"  önload="scrollToBottom()" >
                   <ul>
                       @foreach(var row in db.Query("SELECT * FROM Messages WHERE (FromUserID= @0 AND ToUserID= @1) OR (FromUserID= @1 AND ToUserID =@0)",id,WebSecurity.CurrentUserId))
                       {
                       if(row.FromUserID==id)
                       {
                        var a= db.QuerySingle("SELECT * FROM UserProfile WHERE UserId=@0",row.FromUserId);

                       <li >
                          <span id="messageToMe">@a.User</span>
                           <br>@row.Message
                       </li>
                       }
                       else
                       {
                       <li >
                           <span id="messageFromMe">@WebSecurity.CurrentUserName</span> <br>
                           @row.Message
                       </li>
                       }

                       }


                   </ul>
               </div>

and CSS
#message 
{
float: right;    
overflow: auto;
position: relative;
border-bottom: 1px solid black;
height: 280px;
width: 450px;    
}
Posted
Comments
Do you see any errors in FireBug Console tab?
What browsers you are testing on?

See Scroll to bottom of Div on page load (jQuery)[^]

* NOTE: this code is using jQuery
Without using jQuery, you can try this:
JavaScript
window.onload=function () {
     var objDiv = document.getElementById("MyDivElement");
     objDiv.scrollTop = objDiv.scrollHeight;
}






Cheers,
Edo
 
Share this answer
 
v3
Comments
wirec aaht 5-Aug-13 11:55am    
if it would be possible I would rather not to use jQuery. :)
Joezer BH 5-Aug-13 11:59am    
See update then :) it should work.
wirec aaht 5-Aug-13 12:08pm    
thanks a lot :)
Joezer BH 5-Aug-13 12:11pm    
You are welcome!
ridoy 5-Aug-13 12:08pm    
My 5!
document.getElementById('message').scrollTop = 9999999;


Quote:
The scrollTop property specifies the scrolling offset in pixels from the top of the region. Setting it to a very large value will force it to the bottom.


More info check this : http://stackoverflow.com/questions/4606560/div-overflow-scroll-to-bottom-is-it-possible[^]

I hope this will help to you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900