title: Bootstrap Scrollspy Uncaught TypeError: Cannot read property 'top' of undefined
body: <pre><code>Uncaught TypeError: Cannot read property 'top' of undefined
  </code></pre>
  
  <p>My code</p>
  
  <pre><code>$("#navbar-collapse ul li a[href^='#']").on('click', function(e) {
  
     // store hash
     var hash = this.hash;
  
     // prevent default anchor click behavior
     e.preventDefault();
  
     // animate
     $('html, body').animate({
         scrollTop: $(hash).offset().top
       }, 300, function(){
  
         // when done, add hash to url
         // (default click behaviour)
         window.location.hash = hash;
       });
  
  });
  </code></pre>
  
  <p>Here is my header html structure</p>
  
  <pre><code>&lt;header class="main-header"&gt;
  &lt;nav class="navbar navbar-static-top" role="navigation"&gt;
      &lt;div class="container"&gt;
          &lt;div class="navbar-header"&gt;
              &lt;a href="/" class="navbar-brand"&gt;Testing&lt;/a&gt;
              &lt;button type="button" class="navbar-toggle collapsed" data-target="#navbar-collapse"&gt;
                  &lt;i class="fa fa-bars"&gt;&lt;/i&gt;
              &lt;/button&gt;
          &lt;/div&gt;
  
          &lt;div class="collapse navbar-collapse" id="navbar-collapse"&gt;
              &lt;ul class="nav navbar-nav"&gt;
                  &lt;li class="dropdown"&gt;
                      &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false"&gt;Scrollspy Testing&lt;/a&gt;
                      &lt;ul class="dropdown-menu" role="menu"&gt;
                          &lt;li&gt;&lt;a href="#"&gt;Testing&lt;/a&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a href="#"&gt;Testing&lt;/a&gt;&lt;/li&gt;
                          &lt;li class="divider"&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a href="#"&gt;See all products&lt;/a&gt;&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/li&gt;
                  &lt;li class="dropdown"&gt;
                      &lt;a href="#" class="dropdown-toggle" data-toggle="dropdown" aria-expanded="false"&gt;Testing Info&lt;/a&gt;
                      &lt;ul class="dropdown-menu"&gt;
                          &lt;li&gt;&lt;a href="#blog"&gt;Blog&lt;/a&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a href="#about"&gt;About Us&lt;/a&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a href="#contact"&gt;Contact Us&lt;/a&gt;&lt;/li&gt;
                          &lt;li&gt;&lt;a href="#support"&gt;Support&lt;/a&gt;&lt;/li&gt;
                      &lt;/ul&gt;
                  &lt;/li&gt;
              &lt;/ul&gt;
          &lt;/div&gt;
      &lt;/div&gt;
  &lt;/nav&gt;
  </code></pre>
  
  <p></p>
  
  <p>Any idea my I'm always getting that error when clicking the dropdown menu of bootstrap</p>