I have a website with a javaScript function that should scroll to the section on the page when the user clicks the navigation item. This script worked before I made changes to my navigation menu. I cannot figure out how to properly refer to ID in javaScript.
Here is the HTML navigation menu:
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle Navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="#">Data Detective</a> </div> <div class="navbar-collapse collapse"> <ul class="nav navbar-nav navbar-right"> <li><a id="home" href="#homeSect">HOME</a></li> <li><a id="about" href="#aboutSect">ABOUT</a></li> <li><a id="portfolio" href="#portfolioSect">PORTFOLIO</a></li> <li><a id="contact" href="#contactSect">CONTACT</a></li> </ul> </div> </div> </div>
Here is javaScript:
$(document).ready(function() { setBindings(); }); //Allow screen to Scroll to selected section function setBindings() { $("nav ul li a").click(function (tip) { tip.preventDefault(); var sectionID = "#" + tip.currentTarget.id + "Sect"; alert('button id ' + sectionID); $("html body").animate({ scrollTop: $(sectionID).offset().top }, 1000); }); }
source share