Is this code correct?

function gotoPage(id) {
    ("html, body").animate({
        scrollTop: $("."+id).offset().top
    }, 2000);

What I am trying to do is. That I created DIVs, with 100% of height, they have certain classes ..
I included jQuery, and I wanted to click on anchor and scroll to that certain page.
<a href="#" onClick="gotoPage(id that I want scroll to)">Lipsum</a>

Does it work? Nope.
What's wrong with it?

Edited by RikTelner

4 Years
Discussion Span
Last Post by Bob Hensley

Does it work? Nope.
What's wrong with it?

A very good tool when writing front-end code is the developer console. Chrome and Firefox have this built in. I would encourage you to seek it out and use it religiously. You'll catch any and all JavaScript errors and where they're occuring. You can also then use console.log() to track down pesky issues throughout your script.

Anyways, two issues with your code:

("html, body").animate({

You need to begin this statement with the $ character. Otherwise you're going to get an error about an undefined function.


This is actually going to search all elements for those with a class of the given ID. Not ID, as you're intending. The selector notation for an ID is the pound sign (#)- just as it is in CSS.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.