David542
David542

Reputation: 110163

ScrollTo within a div on a page

I am trying to scroll down a page to load more content. Normally I would do the following:

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

However, I have a page where it is only a left sidebar that needs to be scrolled, and doing something like the following does not work. See image here --

enter image description here

How would I scroll down the left sidebar? The xpath of the containing element is: //li[contains(@class, "search-result")]

Upvotes: 0

Views: 293

Answers (3)

Unmitigated
Unmitigated

Reputation: 89234

Use this:

var objDiv = document.getElementById("your_div"); objDiv.scrollTop = objDiv.scrollHeight;

Upvotes: 1

Pal Singh
Pal Singh

Reputation: 1974

It is similar to your approach, you can simply do

document.getElementById(YOUR_HTML_DIV).scrollTop = SCROLL_VALUE_INT;

JSFIDDLE

Upvotes: 1

Jay Rich
Jay Rich

Reputation: 34

So here we you are scrolling 'window' to 'document.body.scrollHeight'.

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

.scrollTo() and .scrollHeight are a method/property on HTML elements. Just need to do the same thing, but with the target div as the element instead of the window/document.body. Can get it with document.findById or jQuery etc.

Upvotes: 0

Related Questions