<p>I've got a div that uses overflow:auto to keep the contents inside the div as it is resized and dragged around the page. I'm using some ajax to retrieve lines of text from the server, then append them to the end of the div, so the content is growing downwards. Every time this happens, I'd like to use JS to scroll the div to the bottom so the most recently added content is visible, similar to the way a chat room or command line console would work.</p>

<p>So far I've been using this snippet to do it (I'm also using jQuery, hence the $() function):</p>

<pre><code>$("#thediv").scrollTop = $("#thediv").scrollHeight;
</code></pre>

<p>However it's been giving me inconsistent results. Sometimes it works, sometimes not, and it completely ceases to work if the user ever resizes the div or moves the scroll bar manually.</p>

<p>The target browser is Firefox 3, and it's being deployed in a controlled environment so it doesn't need to work in IE at all.</p>

<p>Any ideas guys? This one's got me stumped. Thanks!</p>

[{'answer_id': 13365, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 7, 'selected': True, 'text': '<p><code>scrollHeight</code> should be the total height of content. <code>scrollTop</code> specifies the pixel offset into that content to be displayed at the top of the element\'s client area.</p>\n\n<p>So you really want (still using jQuery):</p>\n\n<pre><code>$("#thediv").each( function() \n{\n   // certain browsers have a bug such that scrollHeight is too small\n   // when content does not fill the client area of the element\n   var scrollHeight = Math.max(this.scrollHeight, this.clientHeight);\n   this.scrollTop = scrollHeight - this.clientHeight;\n});\n</code></pre>\n\n<p>...which will set the scroll offset to the last <code>clientHeight</code> worth of content.</p>\n'}, {'answer_id': 14494, 'author': 'Vincent Robert', 'author_id': 268, 'author_profile': 'https://Stackoverflow.com/users/268', 'pm_score': 3, 'selected': False, 'text': '<p>Using a loop to iterate over a jQuery of one element is quite inefficient. When selecting an ID, you can just retrieve the first and unique element of the jQuery using get() or the [] notation.</p>\n\n<pre><code>var div = $("#thediv")[0];\n\n// certain browsers have a bug such that scrollHeight is too small\n// when content does not fill the client area of the element\nvar scrollHeight = Math.max(div.scrollHeight, div.clientHeight);\ndiv.scrollTop = scrollHeight - div.clientHeight;\n</code></pre>\n'}, {'answer_id': 741982, 'author': 'troelskn', 'author_id': 18180, 'author_profile': 'https://Stackoverflow.com/users/18180', 'pm_score': 5, 'selected': False, 'text': '<p><a href="https://developer.mozilla.org/en/DOM/element.scrollIntoView" rel="noreferrer"><code>scrollIntoView</code></a></p>\n\n<blockquote>\n  <p>The <strong>scrollIntoView</strong> method scrolls the element into view. </p>\n</blockquote>\n'}, {'answer_id': 7605343, 'author': 'peterbrown', 'author_id': 972295, 'author_profile': 'https://Stackoverflow.com/users/972295', 'pm_score': 3, 'selected': False, 'text': '<pre><code>$("#thediv").scrollTop($("#thediv")[0].scrollHeight);\n</code></pre>\n'}, {'answer_id': 19598983, 'author': 'ObjectiveTC', 'author_id': 802196, 'author_profile': 'https://Stackoverflow.com/users/802196', 'pm_score': -1, 'selected': False, 'text': '<p>I had a div wrapping 3 divs that were floating left, and whose contents were being resized.  It helps to turn funky-colored borders/background on for the div-wrapper when you try to resolve this.  The problem was that the resized div-content was overflowing outside the div-wrapper (and bled to underneath the area of content below the wrapper).  </p>\n\n<p>Resolved by using @Shog9\'s answer above.  As applied to my situation, this was the HTML layout:</p>\n\n<pre><code>&lt;div id="div-wrapper"&gt;\n  &lt;div class="left-div"&gt;&lt;/div&gt;\n  &lt;div id="div-content" class="middle-div"&gt;\n  Some short/sweet content that will be elongated by Jquery.\n  &lt;/div&gt;\n  &lt;div class="right-div"&gt;&lt;/div&gt;\n&lt;/div&gt;\n</code></pre>\n\n<p>This was the my jQuery to resize the div-wrapper:</p>\n\n<pre><code>&lt;script&gt;\n$("#div-content").text("a very long string of text that will overflow beyond the width/height of the div-content");\n//now I need to resize the div...\nvar contentHeight = $(\'#div-content\').prop(\'scrollHeight\')\n$("#div-wrapper").height(contentHeight);\n&lt;/script&gt;\n</code></pre>\n\n<p>To note, $(\'#div-content\').prop(\'scrollHeight\') produces the height that the wrapper needs to resize to.  Also I am unaware of any other way to obtain the scrollHeight an actual jQuery function; Neither of $(\'#div-content\').scrollTop() and $(\'#div-content\').height would produce the real content-height values.  Hope this helps someone out there!</p>\n'}, {'answer_id': 55489459, 'author': 'daniels', 'author_id': 2270233, 'author_profile': 'https://Stackoverflow.com/users/2270233', 'pm_score': 1, 'selected': False, 'text': '<p>It can be done in plain JS. The trick is to set scrollTop to a value equal or greater than the total height of the element (<code>scrollHeight</code>):</p>\n\n<pre><code>const theDiv = document.querySelector(\'#thediv\');\ntheDiv.scrollTop = Math.pow(10, 10);\n</code></pre>\n\n<p>From <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollTop#Syntax" rel="nofollow noreferrer">MDN</a>:</p>\n\n<blockquote>\n  <p>If set to a value greater than the maximum available for the element,\n  scrollTop settles itself to the maximum value.</p>\n</blockquote>\n\n<p>While the value of <code>Math.pow(10, 10)</code> did the trick using a too high value like <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Infinity" rel="nofollow noreferrer"><code>Infintiy</code></a> or <code>Number.MAX_VALUE</code> will reset scrollTop to <code>0</code> (Firefox 66).</p>\n'}]
['https://Stackoverflow.com/questions/13362', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1384/']