views:

152

answers:

3

How can I get the active index of the jquery accordion pane when a button is clicked? What I want to do is when a button is clicked in pane 3 for example, I want to store that value and when the page is reloaded, I want pane 3 to remain open.

I intially had this in my server side click and when I hard code a value in for paneIndex it works fine, but obviously, I don't want to do this, I want to get the index on the click and pass that to the script.

string script = "<script type=\"text/javascript\">var paneIndex = " + 3 + "</script>";

if(!ClientScript.IsStartupScriptRegistered("JSScript"))
    ClientScript.RegisterStartupScript(this.GetType(),"JSScript", script);
A: 

You could store the value in a hidden form field, and assuming you are doing a postback, that information will now be in the hidden field for you to use on the server side.

Morcalavin
Can you give an example with doing this with the accordion pane?
Xaisoft
I'm not a jquery exert, but someone else has already demonstrated how to accomplish this.Using their example(Brida Batchelder's), all you need to do on the server side is reference Request.Form['myHiddenInputName']
Morcalavin
A: 

You will want to bind a function to the change event of the accordian, and store the new active index into a hidden input so that it gets sent back to the server.

As far as round-tripping the active index back to the HTML that is returned from the server - your approach is fine. Obviously instead of the hardcoded value of 3, you would put in the value from the hidden input.

$("#accordion").accordion({
    active:1,
    change: function(event, ui) {
        var activeIndex = $("#accordion").accordion('option','active');
        $("myHiddenInputId").val(activeIndex);
        //alert(activeIndex);
    }
});

From the server side, you can access the value and push it back to the page in a similar manner as you posted in the question:

string activeIndex = Request.Form["myHiddenInputName"];
string script = string.Format(@"<script type=""text/javascript"">var paneIndex = {0};</script>", activeIndex);

That should do it.

Bryan Batchelder
How can I get the value of the active index to store in the myHiddenInputId from the server-side?
Xaisoft
A: 

What's also a possibility is using the jquery.cookie plugin and storing the active pane index in a cookie. That way, everything actually happens clientside. I don't know if this might be a valid answer, just throwing it out here for completeness sake :D

Tim Geerts