Thomas Spade
Thomas Spade

Reputation: 744

jQuery Uncaught TypeError: Cannot read property 'top' of undefined - Scoll to Top

I'm trying to create a vertical menu that scrolls to sections of the page. I've found a lot of people having a similar issue, but can't find a working solution for me.

Here's my javascript:

//Scroll to Section
        jQuery('.p-pagination span').on('click', function() {
            var sectionID = jQuery(this).attr('id');
            console.log(sectionID);

            if (sectionID.length){

                jQuery('html, body').animate({
                    scrollTop: jQuery(sectionID).offset().top
                }, 1000);


            }

HTML Markup:

<div class="p-pagination">
<span id="951" class="active"></span><span id="956"></span><span id="1119"></span><span id="1123"></span><span id="1117"></span><span id="1114"></span><span id="1112"></span><span id="1110"></span><span id="1099"></span>
<span id="953"></span><span id="946"></span><span id="1129"></span>
</div>

Most of the solutions I have found were to add a check on the elements length, but it's not working for me. I keep getting the error.

Thanks.

Upvotes: 0

Views: 966

Answers (1)

Shahaf Antwarg
Shahaf Antwarg

Reputation: 497

try to change the line to that:

scrollTop: jQuery('#'+sectionID).offset().top

Upvotes: 1

Related Questions