<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?
This worked for me :
Another way to do it with a smooth animation is like this
As per François Noël's answer "For those who still can't make this work, make sure that the overflowed element is displayed before using the jQuery function."
I had been working in a bootstrap modal that I repeatedly bring up with account permissions in a div that overflows on the y dimension. My problem was, I was trying to use the jquery .scrollTop(0) function and it would not work no matter how I tried to do it. I had to setup an event for the modal that didn't reset the scrollbar until the modal had stopped animating. The code that finally worked for me is here:
See the
scrollTop
attribute.these two codes worked for me like a charm this first will take to scroll to the top of the page but if you want to scroll to some specific div use the second one with your div id.
If you want to scroll to by a class name use the below code
If the html content overflow a single viewport, this worked for me using only javascript:
Regards,