Search code examples
javascripthtmlscroll

Scroll back to the top of scrollable div


<div id="containerDiv"></div>
#containerDiv {
  position: absolute;
  top: 0px;
  width: 400px;
  height: 100%;
  padding: 5px;
  font-size: .875em;
  overflow-y: scroll;
}
document.getElementById("containerDiv").innerHTML = variableLongText;

How to reset the scroll position back to top of container div the next time?


Solution

  • var myDiv = document.getElementById('containerDiv');
    myDiv.innerHTML = variableLongText;
    myDiv.scrollTop = 0;
    

    See the scrollTop attribute.