I am currently developing a live chat website for my users and I am using ajax to fetch the chat messages. However, I am facing an issue where whenever I enter a new message, it does get added to the database and displayed, but the scroll doesn't automatically go to the bottom of the results.
For example, when I input a new message, the scroll fails to reach the bottom.
https://i.sstatic.net/29hYk.jpg
If I manually scroll, then all the messages become visible.
https://i.sstatic.net/qgdg7.jpg
Here is the HTML div where the data is fetched via ajax:
<div id="chat">
</div>
I have tried using this code snippet to resolve the issue, however, it did not work as expected:
window.setInterval(function(){
var elem = document.getElementById('chat');
elem.scrollTop = elem.scrollHeight;
}, 1000);
I would appreciate any suggestions on the best approach to achieve the desired result.