I try to scroll the div of a chat down with JavaScript to the newest message when the user opens the chat, sends or receives a message. For this I tried to use ScrollTop like recommended in many google search results. I made the div element as a global object because I want to use the line with ScrollTop several times in the script. The chatdiv is set to 'overflow-y: scroll;'. I verified with an alert output in the browser console that the window.onload function is working.
<script type="text/javascript">
var element_label = document.getElementById("chatdiv");
window.onload = function WindowLoad(event) {
element_label.scrollTop = element_label.scrollHeight;
}
</script>
My next try was to get the newest message object and use ScrollIntoView on it. This also did not work.
<script type="text/javascript">
var element_label = document.getElementById("chatdiv");
var last_mess = document.getElementById("chatdiv").lastChild;
window.onload = function WindowLoad(event) {
last_mess.scrollIntoView();
}
</script>
Am I missing a significant point or is it no longer possible to scroll a div with JavaScript?
Thanks in advance!