Question

Why is this not saving the cookie when you refresh the page?

http://jsfiddle.net/jBVBL/

Or would it be better practice to save only 1 cookie with the value of every li with ".active"

var hideshow = $(".hideShow");

hideshow.children().not(".active").each(function(index, value){
    var tis = $(this);

    if($.cookie('hideShow_id'+index) == index){
        tis.addClass(".active");
    } else {
        $(this).find("div").hide();
    }            
});

hideshow.find('h3').click(function(e){
    var tis = $(this);
    var tisindex= tis.parents("li").index;
        $.cookie('hideShow_id' + tisindex, tisindex);

        tis.next().slideToggle('2000');
        tis.parent().toggleClass('active');
    e.preventDefault();
});


<ul class="hideShow">
        <li class="active">
         <h3><a href="javascript:;">Pellentesque nec leo cursus ipsum rhoncus volutpat nec eget mi.</a></h3>
         <div>
               <p>Nam velit metus, vulputate eget sodales ut, dignissim vehicula nisi. Lorem ipsum dolor sit amet</p>
             <ul>
                 <li>
                     <strong>Pellentesque</strong> nec leo cursus ipsum rhoncus volutpat nec eget mi.</li>
                 <li>

                     <strong>Nam</strong> quis lectus enim, ac euismod urna.</li>
                 <li>
                     <strong>Donec</strong> varius massa augue, at feugiat tortor.</li>
             </ul>
         </div>
    </li>
    <li>
     <h3><a href="javascript:;">Pellentesque nec leo cursus ipsum rhoncus volutpat nec eget mi.</a></h3>
     <div>
           <p>Nam velit metus, vulputate eget sodales ut, dignissim vehicula nisi. Lorem ipsum dolor sit amet</p>
         <ul>
             <li>
                 <strong>Pellentesque</strong> nec leo cursus ipsum rhoncus volutpat nec eget mi.</li>
             <li>

                 <strong>Nam</strong> quis lectus enim, ac euismod urna.</li>
             <li>
                 <strong>Donec</strong> varius massa augue, at feugiat tortor.</li>
         </ul>
     </div>
    </li>
    <li>
     <h3><a href="javascript:;">Pellentesque nec leo cursus ipsum rhoncus volutpat nec eget mi.</a></h3>
     <div>
           <p>Nam velit metus, vulputate eget sodales ut, dignissim vehicula nisi. Lorem ipsum dolor sit amet</p>
         <ul>
             <li>
                 <strong>Pellentesque</strong> nec leo cursus ipsum rhoncus volutpat nec eget mi.</li>
             <li>

                 <strong>Nam</strong> quis lectus enim, ac euismod urna.</li>

         </ul>
     </div>
    </li>
</ul> 

Using: https://raw.github.com/carhartl/jquery-cookie/master/jquery.cookie.js

Was it helpful?

Solution

You are creating a different cookie for each index, and they never match the one you look for on page load. In addition, you have different cookie name strings 'hideShow-id' vs 'hideShow_id'

OTHER TIPS

 if($.cookie('hideShow-id') == index){

and

 $.cookie('hideShow_id' + tisindex, tisindex);

are clearly two completely different cookies

EDIT:

You are also horribly misusing index. First of all it is a function, not a property. But even then, you are going to get different indexes on load and click because you are confusing two different indexes

tis.parents("li").index()

is the index if the li tag among its siblings, where as

.each(function(index, value)

will give you index within the set you are iterating, which are clearly two different sets, because of the .not(".active") part.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top