Joe Joe - 3 years ago 234
jQuery Question

jQuery UI Accordion + Cookies - Closed by Default?

With the jQuery UI Accordion, I am successful in the use of cookies to remember the last active state when refreshing the page- however I am unable to:


  1. set to collapsed on first visit

  2. have cookie remember if the user has manually collapsed after opening



I've tried to attempt creating a function with conditional statement on the "active" option but was unsuccessful.

http://jsfiddle.net/77xC9/18/

jQuery(document).ready(function(){
var act = 0;
$( "#accordion" ).accordion({
create: function(event, ui) {
//get index in cookie on accordion create event
if($.cookie('saved_index') != null){
act = parseInt($.cookie('saved_index'));
}
},
change: function(event, ui) {
//set cookie for current index on change event
$.cookie('saved_index', null);
$.cookie('saved_index', ui.options.active);
},
active:parseInt($.cookie('saved_index')),
collapsible: true
});
});


‚Äč

Answer Source
jQuery(document).ready(function(){
    $( "#accordion" ).accordion({
        change: function(event, ui) {
            $.cookie('saved_index', ui.options.active !== false ? ui.options.active : null);
        },
        active: $.cookie('saved_index') != null ? parseInt($.cookie('saved_index')) : false,
        collapsible: true
    });
});

ps: Google Chrome does not accept cookies if you open your page as a local file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download