title: Move scrollbar programmatically based on selection - Javascript
body: <p>I have an autocomplete box that populates a list of options.</p>
  
  <p>An image is as follows</p>
  
  <p><a href="https://i.stack.imgur.com/mKamW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/mKamW.png" alt="enter image description here"></a></p>
  
  <p>The list options will be <strong>scrolled down using down arrow</strong>. When the down arrow is pressed, I need to programmatically move the scrollbar based on the selection.</p>
  
  <p>The code is as follows.</p>
  
  <p>HTML</p>
  
  <pre><code>&lt;div id="search-location-result" class="autocomplete-active"&gt;
     &lt;ul class="list-group" id="search-result-display-id"&gt;
       &lt;li&gt;Dallas, Texas United States&lt;/li&gt;
       &lt;li&gt;Dallas, Texas United States. A long address&lt;/li&gt;
       &lt;li&gt;Dallas, Texas United States. A long address&lt;/li&gt;
       &lt;li&gt;Dallas, Texas United States. A long address&lt;/li&gt;
       &lt;li&gt;Dallas, Texas United States. A long address&lt;/li&gt;
       &lt;li&gt;Dallas, Texas United States. A long address&lt;/li&gt;
       &lt;li&gt;Dallas, Texas United States. A long address&lt;/li&gt;
     &lt;/ul&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Javascript</p>
  
  <pre><code>$(locationField).on('keydown', function(e) {
  
  
        var key = e.keyCode,
        $selected = $(listItems).filter('.selected'),
        $current;
        // Down key press
        if ( key == 40 ) {
          if ( ! $selected.length || $selected.is(':last-child') ) {
            $current = $(listItems).eq(0);
          }
          else {
            $current = $selected.next();
          }
          var currentScroll = $current.position().top;
          $(".list-group").scrollTop(currentScroll); 
        }
  
      });
  </code></pre>
  
  <p>The issue is that the scrollbar is not appearing correctly based on the selection.</p>
  
  <p>That is, If we consider the attached image, </p>
  
  <ul>
  <li><p>the scrollbar needs to stay on top for the first four locations</p></li>
  <li><p>the scrollbar needs to move when the down arrow selects 'Dallas<br>
  Kortukeli Antalya, Turkey'.</p></li>
  </ul>
  
  <p>Any idea on how to fix this?</p>