javaScript .splice() not working on correctly

Posted by adardesign on Stack Overflow See other posts from Stack Overflow or by adardesign
Published on 2010-04-15T17:41:11Z Indexed on 2010/04/15 17:43 UTC
Read the original article Hit count: 283

Filed under:
|
|
|

I am setting a cookie for each navigation container that is clicked on.

It sets an array that is joined and set the cookie value. if its clicked again then its removed from the array.

It somehow buggy.

It only splices after clicking on other elements. and then it behaves weird.

Thanks much.

var navLinkToOpen;
var setNavCookie = function(value){
    var isSet = false;
    var checkCookies = checkNavCookie()
    setCookieHelper = checkCookies? checkCookies.split(","): [];
    console.log("value passed", value)
    for(i in setCookieHelper){
        if(value == setCookieHelper[i]){
            setCookieHelper.splice(value,1);
            isSet = true;
        }
    }
    if(!isSet){
        setCookieHelper.push(value)
        }
        setCookieHelper.join(",")
        document.cookie = "navLinkToOpen"+"="+setCookieHelper;
}


var checkNavCookie = function(){
        var allCookies = document.cookie.split( ';' );
        for (i = 0; i < allCookies.length; i++ ){
                temp = allCookies[i].split("=")
                if(temp[0].match("navLinkToOpen")){
                    var getValue = temp[1]
                    }
                }
        return getValue || false
}



$(document).ready(function() {
  $("#LeftNav li").has("b").addClass("navHeader").not(":first").siblings("li").hide()
  $(".navHeader").click(function(){
 $(this).toggleClass("collapsed").nextUntil("li:has('b')").slideToggle(300);
  setNavCookie($('.navHeader').index($(this)))
 return false
  })    

console.log("init",document.cookie)
var testCookies = checkNavCookie();
if(testCookies){
    finalArrayValue = testCookies.split(",")
    for(i in finalArrayValue){
 $(".navHeader").eq(finalArrayValue[i]).toggleClass("collapsed").nextUntil(".navHeader").slideToggle   (0);
        }

}
});

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about arrays