Amr Elgarhy Amr Elgarhy - 6 months ago 19
HTML Question

How to scroll to an element inside a div?

I have a scrolled

div
and I want to have a link when I click on it it will force this
div
to scroll to view an element inside.
I wrote its JavasSript like this:

document.getElementById(chr).scrollIntoView(true);


but this scrolls all the page while scrolling the
div
itself.
How to fix that?

I want to say it like that

MyContainerDiv.getElementById(chr).scrollIntoView(true);

Answer

You would have to find the poistion of the element in the DIV you want to scroll to, and set the scrollTop property.

divElem.scrollTop = 0;

Update:

Sample code to move up or down

  function move_up() {
    document.getElementById('divElem').scrollTop += 10;
  }

  function move_down() {
    document.getElementById('divElem').scrollTop -= 10;
  }
Comments