0

Everytime I add a new element the page should scroll to the bottom of the inner div (having id = content). Right now scrollTo scrolls to the top of the outer div aka container

https://jsfiddle.net/fLd7mxpq/

// css
    .container{
      background: #eee;
      height: 800px;
    }
    .inner-div{
      overflow-y: auto;
      height: 100px;
      background: #aaa;
    }
<script>
function addItem() {
    document.getElementById('content').innerHTML += "<p>Some new Data</p>";
  window.scrollTo(0,0);
}
</script>
<div class="container">
  <div class="inner-div" id="content">
  <p>
  Some random data
  </p>
  <p>
  Some random data
  </p>
  <p>
  Some random data
  </p>
  <p>
  Some random data
  </p>
  <p>
  Some random data
  </p>
  </div>
  <div>
  <p>
  Some random data
  </p>
  <p>
  Some random data
  </p>
  <button onclick="addItem()">
  Add Item
  </button>
  </div>
</div>
Etherealm
  • 2,234
  • 3
  • 18
  • 34
  • isn't 0,0 just the top left? also, does this not work? http://stackoverflow.com/questions/270612/scroll-to-bottom-of-div – Matthias Apr 04 '17 at 13:35
  • @Matthias Is correct. That answer in the linked question will solve your issue. For completeness sake, here is your code using a similar scheme: https://jsfiddle.net/7de36k19/ – Joseph Marikle Apr 04 '17 at 13:41
  • No, this doesn't work. – Etherealm Apr 04 '17 at 13:41
  • @Etherealm can you explain? Are you trying to scroll the inner div or the outer div? – Joseph Marikle Apr 04 '17 at 13:42
  • I'm trying to scroll the inner div to bottom. The height of outer div doesn't change if content is added to inner div. Right now when I try the above mentioned code, the outer div gets scrolled. – Etherealm Apr 04 '17 at 13:46
  • In the linked fiddle from my comment, it scrolls `.inner-div`, not `.container` nor window. Is that not correct? – Joseph Marikle Apr 04 '17 at 13:50
  • https://jsfiddle.net/7de36k19/2/ Updated the jsfiddle. Now clicking add button should scroll to the new item added. – Etherealm Apr 04 '17 at 13:56

0 Answers0