0

I have the live demo of the code here: LIVE DEMO

What I am looking to do is when the 1 box is clicked it should scroll to the first section, when the 2 box is clicked it should scroll to the second section and so forth. But when I click on the numbers, I receive the following error:

offset().top is null or not an object

My JQuery looks like:

$(function() {
    $(".pointto").mouseover(function() {
        $(this).addClass("Hover");
    });
    $(".pointto").mouseout(function() {
        $(this).removeClass("Hover").removeClass("Pressed");
    });
    $(".pointto").mousedown(function() {
        $(this).addClass("Pressed");
    });
    $(".pointto").mouseup(function() {
        $(this).removeClass("Pressed");
    });
$(document).on('click', '.Hover, .Pressed, .pointto', function() { 
    var nn = $(this).attr('id').replace('s', '');
    alert('a[name="'+nn+'"]'); //clicking on 1 gives me <a name="01">
    t = $('a[name="'+nn+'"]').offset().top; //t = $('a[name="'+nn+'"]').offset().top;
    $('body,html').animate({scrollTop:t},800);
});
});

HTML sample:

<a id="s01" class="pointto">1</a> //clickable link
...
...
...
<a name="01">1. About</a> //target

Any idea how to resolve that error?

Update: [RESOLVED]

$(document).on('click', '.Hover, .Pressed, .pointto', function() { 
    var nn = $(this).attr('id').replace('s', '');
    alert('a[name="'+nn+'"]');
    t = $('a[name="'+nn+'"]').offset().top; //t = $('a[name="'+nn+'"]').offset().top;
    $('body,html').animate({scrollTop:t},800);
});
4
  • Have you tried this t = $('a[name="'+nn+'"]')[0].offset().top; ? Commented Jun 20, 2013 at 20:08
  • 1
    In your dev console, what do you get when you enter $('a[name="'+nn+'"]'); I think the selector can't find an element Commented Jun 20, 2013 at 20:08
  • Sounds like your selector isn't matching any elements Commented Jun 20, 2013 at 20:09
  • @Ghillied If that edit is done then I receive an missing ')' error Commented Jun 20, 2013 at 20:10

3 Answers 3

2

Your selector is returning empty. Your target elements have names such as "1", "2", etc. and your selector is trying to find "s01", "s02" etc.

Your code should be var nn = $(this).attr('id').replace('s0', ''); or rename the elements you want to scroll to appropriately.

Sign up to request clarification or add additional context in comments.

Comments

1

Based on your posted html, this is what you have:

var nn = $(this).attr('id').replace('a', '');

Since the id is s01, nn is now set to s01, since there are no "a"s in the id, nothing is replaced

t = $('a[name="'+nn+'"]').offset().top;

You then look for an a with a name of s01. Your anchor does not have this name, your anchor is named 1. The offset of nothing is null, so you then try to take the top property of null.

1 Comment

your selector was fine before, your name value was just wrong.
1

Your selector when doing $('<a name="'+nn+'">') is wrong. it should be :

$('a[name="'+nn+'"]')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.