Livog
Livog

Reputation: 195

jQuery Accordion if hasClass

I am trying to make a accordion, and I want the H4 element with the class current-category to be expanded first.

My code:

$(function() {
     $( "#category-accordion" ).accordion();
     if ($('h4').hasClass("current-category")) {
        $( "#category-accordion" ).accordion("activate", $('.current-category')); }
});

And yes I am a noob.

Thanks in advance!

Upvotes: 1

Views: 623

Answers (1)

Felix Kling
Felix Kling

Reputation: 816262

activate is an event [docs] which gets triggered when a panel has been activated. I believe you want to use the active option [docs], which takes the index of the element you want to be opened:

$("#category-accordion").accordion({
    active: $("#category-accordion > div").index($('h4.current-category').next())
});

I assume your HTML looks like:

<div id="category-accordion">
    <h4>Section 1</h4>
    <div>...</div>
    <h4>Section 2</h4>
    <div>...</div>
</div>

DEMO


The jQuery documentation usually contains enough information and examples to solve such problems.

Upvotes: 2

Related Questions