How to scroll back to the top of scrollable div with JavaScript?

Spread the love

Sometimes, we want to scroll back to the top of scrollable div with JavaScript.

In this article, we’ll look at how to scroll back to the top of scrollable div with JavaScript.

How to scroll back to the top of scrollable div with JavaScript?

To the top of scrollable div with JavaScript, we set the scrollTop property of the element to 0.

For instance, we write

const myDiv = document.getElementById("containerDiv");
myDiv.innerHTML = variableLongText;
myDiv.scrollTop = 0;

to select the scroll container with getElementById.

Then we set its scrollTop property to 0 to scroll to the top of it.

Conclusion

To the top of scrollable div with JavaScript, we set the scrollTop property of the element to 0.

Leave a Reply

Your email address will not be published.