tags:

views:

779

answers:

4

Hi there,

Can anyone inform me how to maintain the jquery accordion active state panel when changing pages. Ideally i would like to change in the code-behind however really happy to just get it working.

developing in asp.net 3.5

Hope this helps

Thanks

+3  A: 

Example here. If you select one of the accordion headers then refresh the page the last accordian panel you clicked is opened by default

I see this as a pure client responsibility. I would store the information in a cookie plugin here which you can read and pass to the accordion constructor. I would prefer this over passing values to and from the server for no real benefit.

Something along these lines

//get persisted active accoridan index
var activeIndex  = $.cookie('accordianActiveIndex');

//guard code here to check you have a valid activeIndex...

$('.selector').accordion({
       active: activeIndex,
       change: function(event, ui) { 
           //set cookie here to new active header (index)
           $.cookie('accordianActiveIndex', ui.newHeader.prevAll().length)
      }
});
redsquare
should i simply copy and paste this below my function for the accordion. Sorry this is my first time using jquery. Thanks for your reply.
First you will need to download and include the plugin script. The you could give it a try but it is very much psuedo code..!
redsquare
i have downloaded the lpugin and pasted in the code supplied, no luck.can you help ?
Is the selector correct? Note I just used the generic .selector.A link to the page or a copy of the code you have would help...
redsquare
i have been trying this out on just the demo accordion. please see code below: <div id="accordion"> <div> <h3><a href="#">First</a></h3> <div>Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet.</div> </div> <div> <h3><a href="#">Second</a></h3> <div>Phasellus mattis tincidunt nibh.</div> </div> <div> <h3><a href="#">Third</a></h3> <div>Nam dui erat, auctor a, dignissim quis.</div> </div> </div>i simply changed the .selector to #accordion and added: header: "h3",can you help ? Thanks
updated my answer with a working demo
redsquare
I also found this example to be very useful to my same issue!
Bill Sambrone
To get this to work I had to add "activeIndex = parseInt(activeIndex, 10);" (posted below with credit to redsquare)
Paul
A: 

For anyone with a similar problem getting cookies to work with jQuery UI Accordion, I've solved it by adding one line to redsquare's code.

The cookie value activeIndex needs to be parsed as an integer:

//get persisted active accoridan index
var activeIndex  = $.cookie('accordianActiveIndex');
activeIndex = parseInt(activeIndex, 10);

//guard code here to check you have a valid activeIndex...

$('.selector').accordion({
       active: activeIndex,
       change: function(event, ui) { 
           //set cookie here to new active header (index)
           $.cookie('accordianActiveIndex', ui.newHeader.prevAll().length)
      }
});
Paul
A: 

And here's another way to save UI Accordion state using the cookie.js plugin:

(source)

var accordion = $("#accordion");
var index = $.cookie("accordion");
var active;
if (index !== null) {
    active = accordion.find("h3:eq(" + index + ")");
} else {
    active = 0
}
accordion.accordion({
    header: "h3",
    event: "click hoverintent",
    active: active,
    change: function(event, ui) {
        var index = $(this).find("h3").index ( ui.newHeader[0] );
        $.cookie("accordion", index, {
            path: "/"
        });
    },
    autoHeight: false
});
Paul
A: 

I Just use

$( ".selector" ).accordion({ navigation: true });

that maintains the status of the selected option

tecnocrata