Scroll to bottom of div?

dMix picture dMix · Nov 6, 2008 · Viewed 849.1k times · Source

I am creating a chat using Ajax requests and I'm trying to get messages div to scroll to the bottom without much luck.

I am wrapping everything in this div:

#scroll {
    height:400px;
    overflow:scroll;
}

Is there a way to keep it scrolled to the bottom by default using JS?

Is there a way to keep it scrolled to the bottom after an ajax request?

Answer

Jeremy Ruten picture Jeremy Ruten · Nov 6, 2008

Here's what I use on my site:

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