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") });

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
Layout: Shane Zehnder ::: BlogCFC was created by Raymond Camden. ::: This blog is running version 5.9.