I am a complete newbie, so excuse my inability to see the obvious solution (if any). However, I checked interweb for the answer to this question and ran into the same question. So far I have worked: using what I found at http://www.tobypitman.com/multiple-collapsable-panels-with-cookies/ , I managed to get some containers to switch between display:blockand display:none, and I set cookies with using the Klaus Hartl cookie.js.
Everything works horribly! Except that I want the initial state of the switch containers to be closed. I really would like to do this without having it display:nonedirectly in CSS, so the content remains available with JS disabled. I am not a programmer, and my brute force method of changing things here and there until something happens does not completely cut it. I have included HTML, CSS and jQuery all the below - the only thing that will not be in my example is a sprite with a CSS image for <h6>, which serves as a trigger.
Toggle with cookie
<style>
.toggle-wrapper {
overflow:hidden;
display:block;
}
.toggle-wrapper .toggle-container {
position:relative;
overflow: hidden;
}
.toggle-wrapper h6.trigger {
background: transparent url(images/trigger-sprite.png) no-repeat left top;
height: 15px;
cursor:pointer;
padding:0 0 0 16px;
margin:0;
}
.toggle-wrapper h6.active {
background-position: left bottom;
padding:0 0 0 16px;
}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
jQuery.cookie = function (key, value, options) {
if (arguments.length > 1 && (value === null || typeof value !== "object")) {
options = jQuery.extend({}, options);
if (value === null) {
options.expires = -1;
}
if (typeof options.expires === 'number') {
var days = options.expires, t = options.expires = new Date();
t.setDate(t.getDate() + days);
}
return (document.cookie = [
encodeURIComponent(key), '=',
options.raw ? String(value) : encodeURIComponent(String(value)),
options.expires ? '; expires=' + options.expires.toUTCString() : '',
options.path ? '; path=' + options.path : '',
options.domain ? '; domain=' + options.domain : '',
options.secure ? '; secure' : ''
].join(''));
}
options = value || {};
var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};
$(document).ready(function(){
$("div.toggle-wrapper h6").addClass("active");
var l = $('div.toggle-wrapper h6').length;
var panel = $("div.toggle-wrapper div.toggle-container");
for (c=0;c<=l;c++){
var cvalue = $.cookie('panel' + c);
if ( cvalue == 'closed' + c ) {
$(panel).eq(c).css({display:"none"});
$(panel).eq(c).prev().removeClass('active').addClass('inactive');
};
};
$("div.toggle-wrapper h6.active").toggle(
function () {
var num = $("div.toggle-wrapper h6").index(this);
var cookieName = 'panel' + num;
var cookieValue = 'closed' + num;
$(this).next("div.toggle-container").slideUp(500);
$(this).removeClass('active');
$.cookie(cookieName, cookieValue, { path: '/', expires: 10 });
},
function () {
var num = $("div.toggle-wrapper h6").index(this);
var cookieName = 'panel' + num;
$(this).next("div.toggle-container").slideDown(500);
$(this).addClass("active");
$.cookie(cookieName, null, { path: '/', expires: 10 });
}
);
$("div.toggle-wrapper h6.inactive").toggle(
function () {
var num = $("div.toggle-wrapper h6").index(this);
var cookieName = 'panel' + num;
$(this).next("div.toggle-container").slideDown(500);
$(this).addClass("active");
$(this).removeClass('inactive');
$.cookie(cookieName, null, { path: '/', expires: 10 });
},
function () {
var num = $("div.toggle-wrapper h6").index(this);
var cookieName = 'panel' + num;
var cookieValue = 'closed' + num;
$(this).next("div.toggle-container").slideUp(500);
$(this).removeClass('active');
$.cookie(cookieName, cookieValue, { path: '/', expires: 10 });
}
);
});
</script>
<div class = "toggle-wrapper"> <h6 class = "trigger"> Trigger 1 </ h6> <div class = "toggle-container"> <p> The material is inside here </p> <p> More material </p> <p> More even </p> </ div> </ div> <div class = "toggle-wrapper"> <h6 class = "trigger"> Trigger 2 </ h6> <div class = " toggle-container "> <p> The material is inside here </p> <p> More material </p> <p> More even </p> </ div> </ div> <div class =" toggle-wrapper "> <h6 class =" trigger "> Trigger 3 </ / h6> <div class =" toggle-container "> <p>The material is inside here </p> <p> More material </p> <p> More even </p> </ div> </ div> Code>