0

so im kinda new to javascript and jquery still so im stuck with this little problem. the problem here is the "currentId" variable. this variable gets a value during the scroll event but i need it to remain even after the scroll event has passed and taken into account during the next scroll event insted it just returns to null or undefined.

can anybody point me in the right direction here? cause iv been reading for a day now hahaha.

$("#content").on("scroll resize", function(){    //on scroll

    var pos=$("#frontpage").offset();    //take front page position in variable

    $('.page').each(function(){    //for each div with a class of page

        if(pos.top >= $(this).offset().top && pos.top < $(this).next().offset().top){    //if this divs position is in the correct range

            if (currentId != $(this).attr('id')|| typeof currentId == 'undefined') {    //if currentId Var is diffrent from this divs id or is undefined

                var currentId = $(this).attr('id');    //set currentId Var to this divs id

                $(this).addClass( "foo" );    //add class foo to this div

            }else{
                            //Else do nothing
            };    //endIfElse

        }else{
            $(this).removeClass( "foo" );     //Else remove the foo class from this div
        };                                     //endIfElse

    });                                //end each

});
3
  • $currentId is null, it has no value as of yet Commented Jul 13, 2013 at 19:57
  • 2
    Declare the variable outside the event handler. var currentId = ...; $('#content#).on(...); Commented Jul 13, 2013 at 19:58
  • 1
    Also typeof(foo) === 'undefined' is a bit of a code smell that should have pointed you towards this solution. Instead of checking whether a variable exists, you should create one at the appropriate point with some initial value before it's needed. Commented Jul 13, 2013 at 19:59

2 Answers 2

1

Why not simply putting var currentId simply out of any function or handler? It will not vanish as long as the page is displayed.

Now to make it real persistent, you can store and read in cookies (using document.cookie).

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

1 Comment

for some reason it still returnes to undefined i need the value assigned to the var to stay along with it
1

This code isn't great in general, but to fix your specific problem, put the declaration of the variable outside the code block:

var currentId;
$("#content").on("scroll resize", function() { //on scroll
    var pos = $("#frontpage").offset(); //take front page position in variable
    $('.page').each(function($currentId) { //for each div with a class of page
        if (pos.top >= $(this).offset().top && pos.top < $(this).next().offset().top) { //if this divs position is in the correct range
            if (currentId != $(this).attr('id') || typeof currentId == 'undefined') { //if currentId Var is diffrent from this divs id or is undefined
                currentId = $(this).attr('id'); //set currentId Var to this divs id
                $(this).addClass("foo"); //add class foo to this div
            } else {
                //Else do nothing
            }; //endIfElse
        } else {
            $(this).removeClass("foo"); //Else remove the foo class from this div
        }; //endIfElse
    }); //end each
});

1 Comment

for some reason it still returnes to undefined i need the value assigned to the var to stay along with it

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.