Home > OS >  JQuery why does .offset() not return value?
JQuery why does .offset() not return value?

Time:12-05

Hi i get a JS error in my browser inspector. But i dont understand whats wrong with my Jquery code. And why he consider this variable as undefined ... Could someone help me ?

The code works well; but i would prefer to remove the error: Uncaught TypeError: varposlist is undefined

This is my js code:

 $(function(){
var pane = $('.scroll-pane');
pane.jScrollPane({
    });
var api = pane.data('jsp');

    if(typeof localStorage!='undefined') {
        //Get var from local storage
        var menu = sessionStorage.getItem("menu");
        var yposistock_rule = sessionStorage.getItem("yposistock_rule");
        

        //Check menu view mode
        if(menu == "arbo") {
            // defined top from a#on anchor
            var varposchapter = $('a#on_menuchapter').offset();
            //add - 285 to put at same level than title
            var vartop1 = varposchapter.top -285 -yposistock_rule;
            if(scrollauto_rule == "oui") {
                sessionStorage.removeItem("yposistock_rule");
            };
            api.scrollTo(0, vartop1);
            return false;
             
        }
        else{   
            // defined top from a#on anchor
            var varposlist = $('a#on_menulist').offset();
            //add - 285 to put at same level than title
            var vartop2 = varposlist.top -285 -yposistock_rule;
            if(scrollauto_rule == "oui") {
                sessionStorage.removeItem("yposistock_rule");
            };
            api.scrollTo(0, vartop2);
            return false;
        }
    }
});

Any idea ? Thanks a lot.

CodePudding user response:

The only thing I can think of is this line:

$('a#on_menulist').offset();

The # selects the element with the same ID. Unless you have an element with id=on_menulist, this code cannot find the id which then cannot find the offset.

  • Related