$(document).ready(function(){ var cookie = $.cookie("panelState"), expanded = cookie ? cookie.split("|").getUnique() : [], cookieExpires = 7; // cookie expires in 7 days, or set this as a date object to specify a date // show cookie content // Remember content that was expanded $.each( expanded, function(){ $('#' + this).show(); }) $('li.primary').click(function(){ $(this).toggleClass("open"); $(this).next().slideToggle('300', function(){ updateCookie(this); }); }) // Update the Cookie function updateCookie(el){ var indx = el.id; var tmp = expanded.getUnique(); if ($(el).is(':hidden')) { // remove element id from the list tmp.splice( tmp.indexOf(el.id) , 1); } else { // add index of widget to expanded list tmp.push(el.id); } expanded = tmp.getUnique(); $.cookie("panelState", expanded.join('|'), { expires: cookieExpires } ); } }); // Return a unique array. Array.prototype.getUnique = function(sort){ var u = {}, a = [], i, l = this.length; for(i = 0; i < l; ++i){ if(this[i] in u) { continue; } a.push(this[i]); u[this[i]] = 1; } return (sort) ? a.sort() : a; }