Simil-accordion with cookies

by DannyEnglander

HTML

<script src="http://cachedcommons.org/cache/jquery-cookie/0.0.0/javascripts/jquery-cookie-min.js"></script>
<div id="cont">
<h3 class="trigger" id="frst">Section 1</h3> 
  
 </div>


 <div class="toggle"> 
      <p> Blah blah blah Blah blah blah Blah blah blah Blah blah blah </p> 
   </div>

CSS

.toggle {
 width:100px;
 color:grey;    
}

.trigger {
 cursor:pointer;   
}

JavaScript

$(".toggle").hide();                                         //Hide .toggles

$(window).load(function(){
  $('.toggle').not(':hidden')
      .prev('.trigger').trigger("click");                    //Simulate click on visible .toggle(s) h3(s)
});

$('.trigger').each(function() {                              //For each .trigger
    var theActive = $.cookie($(this).attr('id'));            //Retrieve the cookies
    if (theActive) {                                         //Verify if cookies exist
        $('#' + theActive).parent().next(".toggle").slideDown(700);   //And slide down the respective .toggle
    }

});

$(".trigger").toggle(                                        //Toggle permits alternate clicks
   function() {
    $(this).parent().next('.toggle').slideDown('slow');               //On odd clicks, .toggle slides down...
    $.cookie($(this).attr('id'), $(this).attr('id'));        //...and the cookie is set by its ids.
}, function() {
    $(this).parent().next('.toggle').slideUp('slow');                 //On even clicks, .toggle slides up...
    $.cookie($(this).attr('id'), null);                      //...and the cookie is deleted.
});