Ben
Ben

Reputation: 4319

How do I get the index of a JQuery Accordion from the header row ID?

I've got an Accordion and I want to get the index of a particular header by its ID.

The accordion is generated from a repeater:

<asp:Repeater ID="rptProjectList" OnItemDataBound="rptProjects_ItemDataBound" runat="server">
    <HeaderTemplate>
        <div id="accordion">
    </HeaderTemplate>

    <ItemTemplate>
        <h1 style="margin: 0px" class="accordionHeader" id='<%# Eval("projectCode") %>'></h1>

        <div>
           ....some stuff             
        </div>
    </ItemTemplate>

    <FooterTemplate>
        </div>
    </FooterTemplate>
</asp:Repeater>

I want to be able to open the page with a particular section expanded, given the projectCode as a parameter. e.g. user goes to MyPage.aspx?project=ABC123 and the page loads with the ABC123 project open and the other panels collapsed.

The jQuery I have is:

var activeProject = $(document).getUrlParam("project");

which gets the code.

And

$j('#accordion').accordion({
        active: activeIndex,
        collapsible: true
    });

which sets the Accordion.

The bit I can't figure out is the bit in the middle. I need to be able to get the activeIndex by iterating over the div to get the index of the row with the relevant project code.

Upvotes: 1

Views: 6709

Answers (1)

Ravi Gadag
Ravi Gadag

Reputation: 15861

you can do like this. accordion Active

var active = $( ".selector" ).accordion( "option", "active" ); //getter 



<div id="accordion">
  <h3 class='headAcc'>First header</h3>
  <div>First content panel</div>
  <h3 class='headAcc'>Second header</h3>
  <div>Second content panel</div>
</div>

JQuery

$('#accordion').accordion({
});

//suppose you want to show thee index which starts some text 
var indexToActivate = $('.headAcc:contains("Second header")').index();
alert(indexToActivate);
indexToActivate=indexToActivate-1; //index is zero based for jquery ui. 
$('button').on('click',function(){
    var active = $( "#accordion" ).accordion( "option", "active" ); //getter 
    alert('Current Index is ' + active +" ");

     $( "#accordion" ).accordion( "option", "active",indexToActivate ); //setter

     var active = $( "#accordion" ).accordion( "option", "active" ); //getter 
       alert('Current New Index is ' + active +" ");
});

Working Demo

Upvotes: 6

Related Questions