0

I have a button on my page, that, when clicked adds a new element to a div on the page. When the div reaches it max height, the scrollbar appears. Now, if I keep pressing the button, new elements will appear, and I'd have to scroll down each time I press the button, in order to see the new element.

How can I move the scrollbar to the bottom after an element is added to the div?

Thanks.

0

2 Answers 2

3

It should be

 window.scrollTo(0,document.body.scrollHeight);

As outlined in this Post

Sign up to request clarification or add additional context in comments.

2 Comments

But If I do something like this : document.getElementById("messagesHolder").scrollTo(0,document.getElementById("messagesHolder").scrollHeight); The console outputs : Uncaught TypeError: document.getElementById(...).scrollTo is not a function
Or you could give the bottom most element of your page and id and just set up logic to go to it on that occasion. So, <footer id="gotobottom"></footer> if(blah=blahblah){ www.webpage.com/#gotobottom}. This is just pseudo code by the way.
0

I used .scrollTop and it worked.

document.getElementById("messagesHolder").scrollTop = document.getElementById("messagesHolder").scrollHeight;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.