How to automatically scroll to the end of a div when adding data?

I have a table inside a div with the following style:

 #data { overflow-x:hidden; overflow-y:visible; height:500px; } 

Displays a vertical scroll bar when enough data is added to the table. However, when new data is added, I would like the div element to automatically scroll down.

What JavaScript code can I use for this? If necessary, I open up opportunities in jQuery, but would prefer a JavaScript solution.

+69
javascript jquery html css
Sep 05 2018-11-11T00:
source share
2 answers

If you don't know when the data will be added to #data , you can set the interval to update the scrollTop element to its scrollHeight every couple of seconds. If you control the addition of data, just call the inside of the next function after adding the data.

 window.setInterval(function() { var elem = document.getElementById('data'); elem.scrollTop = elem.scrollHeight; }, 5000); 
+104
Sep 05 '11 at 4:45
source share
 var objDiv = document.getElementById("divExample"); objDiv.scrollTop = objDiv.scrollHeight; 
+33
Sep 05 2018-11-11T00:
source share



All Articles