Error . top in jQuery

Asked

Viewed 30 times

0

$("#j_menu a").click(function(e) {
  var goto = $("#"+$(this).attr('href').replace('#','')+"");
    $('html, body').animate({
        scrollTop: goto.offset().top
    }, 'slow');
});

He accuses a mistake saying he can’t read ". top" but on another site I’m building he recognized.

  • I added a condition to check if the element really exists. if (goto.length) { $("html,body").animate(...) }

  • The id generated by "#"+$(this).attr('href').replace('#','')+"" exists on the page ? Make console.log(goto); exactly following the definition of the variable goto to see what value it is building.

1 answer

0

The appeal .offset().top is called jquery, so you should put the variable inside $() being like this $(goto).offset().top. To get position of the element only with javascript has that question here

Browser other questions tagged

You are not signed in. Login or sign up in order to post.