Alex Borsody
Alex Borsody

Reputation: 2070

Uncaught TypeError: Cannot read property 'top' of null : conflct

I wrote this code to create a floating sidebar that sticks at top of the browser window. I am getting this error in chrome in line 3.

Uncaught TypeError: Cannot read property 'top' of null 

this is causing a plugin I am using not to work.

$(document).ready(function(){
    // Floating sidebars on page nodes
    var sidebartop = $('.page-node .panels-flexible-region-node_layout-right').offset().top;
    $(window).scroll(function(){
        if( $(window).scrollTop() > sidebartop ) {
            $('.page-node .panels-flexible-region-node_layout-right').css({position: 'fixed', top: '40px', margin: '0 0 0 650px'});
        } else {
            $('.page-node .panels-flexible-region-node_layout-right').css({position: 'static', margin: '-35px 0 0 0'});
        }
    });
});

Upvotes: 1

Views: 12740

Answers (1)

Jessey
Jessey

Reputation: 46

I think, jQuery couldn't find specified selector. I'am right if

$('.page-node .panels-flexible-region-node_layout-right')

returns empty array. Try to set some id for your sidebar (or which element you want to get offset) and rewrite code like

var sidebartop = $('#your-selector-id').offset().top;

Also, you can search difference between .first-class .second-class and .first-class.second-class (note for lack of space in second example) query rules.

Upvotes: 3

Related Questions