I had a little problem due to some markdownify plugin, anyway I had to search for the link's target so I used this work around. Its actually working for a variety of element selectors, in my case my link was <a href="#target">some text</a> and my actual target was <a href="target"></a> for this particular case I can give you that solution:
var scrollAnchorSamePage = function() {
$('a[href^="#"]').click(function() {
event.preventDefault();
var id = $(this).attr("href");
// okay lets remove the hashtag in front:
var target = $('a[href^="' + id.substring(1, id.length) + '"]');
// and I need a little offset due to a fixed sticky header by 140
$('html, body').animate({ scrollTop: target.offset().top - (160) }, 1000);
});
}
scrollAnchorSamePage();
PS: I use following imports of jQuery packages, you can add them right before your closing </body> html tag in your DOM.
<!-- Latest minified jQuery -->
<script src="https://code.jquery.com/jquery-2.2.4.min.js" integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44=" crossorigin="anonymous"></script>
<!-- Latest compiled and minified bootstrap JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js" integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS" crossorigin="anonymous"></script>
Uncaught TypeError: $(...).scrollTo is not a function- because jquery does not have ascrollTofunction.