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 19:03 UTC
Read the original article Hit count: 189

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.

It might be that splice is not the correct method Thanks much.

var navLinkToOpen;
var setNavCookie = function(value){
var isSet = false;
var checkCookies = checkNavCookie()
  setCookieHelper = checkCookies? checkCookies.split(","): [];
  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
  }) 

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