jQuery UI Accordion, default expanded

redflag237 picture redflag237 · Apr 3, 2013 · Viewed 8k times · Source

I'm using the jQueryUI Accordion plugin for site navigation. My page is rendered server-side, the current tab gets an attribute defaultactive=true. The markup is as following:

<ul class="accordion">
    <li>One</li>
    <li defaultactive="true">Two</li>
    <li>Three</li>
</ul>

I know this snippet to work as expected:

$("#accordion").accordion({ active: 2 });

What do I have to write to get the exact position (zero-based) of the li-element with defaultactive-attribute within the ul container?

Answer

Fr&#233;d&#233;ric Hamidi picture Frédéric Hamidi · Apr 3, 2013

Since all your <li> elements are siblings, you can use index():

$(".accordion").accordion({
    active: $(".accordion li[defaultactive=true]").index()
});

Note that according to your markup, you should use a class selector (.accordion), not an id selector (#accordion).