bryanlewis bryanlewis - 1 year ago 55
Javascript Question

Cannot read property 'top' of undefined

Trying to get the page to scroll to an anchor and I'm getting this error.

Cannot read property 'top' of undefined

Right now I have my JS like the following...

//scroll to section process page
function scrollToAnchor(aid){
var aTag = $("div[name='"+ aid +"']");
$('html,body').animate({scrollTop: aTag.offset().top},'slow');
}

$("li.menu-item-141 a").click(function() {
scrollToAnchor('#philosophy-page');
});


Here is my HTML...

<div class="container">
<div name="philosophy-page" id="philosophy-page">
<div class="philosophy-heading">
<h1>Philosophy</h1>
</div><!-- /.philosophy-heading -->
</div><!-- /#philosophy-page -->
</div><!-- /.container -->


Any help would be great!

Thank you!

Answer Source

replace

scrollToAnchor('#philosophy-page');

by

scrollToAnchor('philosophy-page');

Remember you use the name to find the a element:

var aTag = $("div[name='"+ aid +"']");

jQuery cannot find an element named #philosophy-page