Home | Projects | Contact Me

Using jQuery to Scroll to the bottom of an Element

I have been working on a little AJAX application using jQuery and ColdFusion using JSON to communicate with the page through components. One of the first things I needed to do was to maximize scroll position of a DIV element each time it was updated. Digging around I finally found what I was looking for.

For my benefit, and for anyone else who needs it, assuming you have a div on the page like so:

<div id="myDiv" style="width: 500px; height: 300px; overflow: auto;"></div>

Using jQuery, each time it is updated we could fix the scroll position:

$("#myDiv").attr({ scrollTop: $("#myDiv").attr("scrollHeight") });

And if you would prefer an animated transition, ljpw suggests using:

$("#myDiv").animate({ scrollTop: $("#myDiv").attr("scrollHeight") }, 3000);

Comments
Oscar's Gravatar Good tutorial, in this case 1 line is more valuable than 10. I'm a jQuery fan so in my current project (an Ajax chat) I started using PREPEND to populate my messages windows chat but when my client requiered me for a more traditional input of messages I decanted for APPEND, then your code comes to make my work easy. Thank you very much!
# Posted By Oscar | 10/1/08 12:32 PM
Nik's Gravatar great work man. Works like a charm!
# Posted By Nik | 10/3/08 6:41 AM
Flemming's Gravatar I've used something very similar, but it doesn't seem to work in Safari (I'm being told, don't have a Mac myself). Have you tested this in Safari?
# Posted By Flemming | 12/19/08 8:29 AM
Shane Zehnder's Gravatar I have tested this on IE and FF in Windows and Linux. I have not tested this on Safari or on a Mac.
# Posted By Shane Zehnder | 12/19/08 9:58 AM
dogmatic's Gravatar OMG... well done... this works like a bomb
# Posted By dogmatic | 2/2/09 3:26 PM
Judd's Gravatar Thanks for posting this, ScrollHeight was just what we were after.
# Posted By Judd | 2/16/09 10:21 AM
ljpw's Gravatar hi thanks for posting this..
# Posted By ljpw | 4/21/09 8:00 AM
ljpw's Gravatar you can be able to animate the scroll using this code.
$("#myDiv").animate({ scrollTop: $("#myDiv").attr("scrollHeight") }, 3000);
# Posted By ljpw | 4/21/09 9:39 PM
Shane Zehnder's Gravatar @ljpw: Nice! Thanks for posting it. I've been wanting to try that out for a while, just haven't had the time to get to it. :)
# Posted By Shane Zehnder | 4/21/09 11:42 PM
Federico Caceres's Gravatar Thank you very much! You just saved me from having to use the horrible horrible document.location.href trick! Great!
# Posted By Federico Caceres | 4/26/09 2:33 AM
Remo Harsono's Gravatar works great :)

I've tried using scrollabe at first, but at last I use this script...definitely this is what I'm looking for :)
# Posted By Remo Harsono | 5/7/09 5:29 AM
sknipas's Gravatar Thanks alot. finally something that works on most browsers.
# Posted By sknipas | 5/15/09 9:21 AM
fun's Gravatar respect man.
# Posted By fun | 6/9/09 11:25 AM
Layout: Shane Zehnder ::: BlogCFC was created by Raymond Camden. ::: This blog is running version 5.9.