0

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.

1 Answer 1

1

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.

2
  • Ok my Bad, this error happend only on home page, where the ID isent define. Thanks for this obvious answer ;) Did you ahve any idea, how i could make the script conditionnal to avoid the error ?
    – casp
    Commented Dec 4, 2021 at 16:25
  • Fixed by adding a condition 'if ( $('#on_menulist).length ) { // exists } else { // doesn't exist }'
    – casp
    Commented Dec 4, 2021 at 16:32

Not the answer you're looking for? Browse other questions tagged or ask your own question.