jQuery UI Accordion activate
Asked Answered
L

14

10

I'm not getting how to do this, or if I can do this. I have a jQuery UI Accordion, multiple sections with each section containing multiple anchor tags each with a unique string id.

I'd like to be able to have the accordion open to where a particular element with a given id is. Like say id "item117". Can I use something like

$('#accordion').activate('activate','#item117');

or even

$('#accordion').activate('activate',117);

I've tried those and some variations but can't get it to go. In the case I was trying to get working, the accordion should've opened to the end of the second section.


I'm still not getting this, so maybe I'm doing something else wrong as well. I've stripped it down to an example page here: http://www.ofthejungle.com/testaccordion.php Please have a look at it and its source.

Livelong answered 14/12, 2009 at 6:47 Comment(2)
Are you using the latest jQuery ui version? I believe the function used to be change not activateTuque
Please note that this question was asked in 2009. When looking at the answers check the date of the response to see if its timely for you.Livelong
C
12

have been over this too & found nice and universal solution
- emulate clicking the header of required item by its id

$('#header-of-item-258').click(); 

that works everytime and anywhere not just accordion

Callous answered 7/9, 2011 at 11:8 Comment(1)
I like this and use it for various odd menu needs. But I like to use the built in functionality where possible. So I've +1 you and @BTR :)Avoirdupois
D
12

From documentation:

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

// setter
$( ".selector" ).accordion( "option", "active", 2 );
Demonography answered 31/1, 2013 at 16:41 Comment(0)
C
10

for me worked

$("#accordion").accordion({active:"h3:last"})
Catalepsy answered 12/6, 2011 at 11:25 Comment(0)
H
9

You need to call it using the function called accordion:

// Open the third set (zero based index)
$('#accordion').accordion('activate', 2); 

To open a section containing a specific element, you would do something like this. Note: you need to target the trigger that normally opens and closes the section. In the docs this is an h3 element, your trigger element may be different, so change this accordingly.

$('#accordion').accordion('activate', '#accordion > div:has(#item117) > h3'); 
Headwaiter answered 14/12, 2009 at 7:4 Comment(3)
is this documented in the jQuery UI API anywhere?Pegram
In jQuery UI 1.10.2 I get the error: no such method 'activate' for accordion widget instancePostremogeniture
I think you mean active, not activate.Ianthe
I
9

This FINALLY worked for me:

$("#accordion").accordion("activate", $("#h3-id"));

NOTE!!! The id has to be that of the <h3> element you want to open up (in the default accordion setup).

Impediment answered 21/3, 2011 at 19:6 Comment(0)
J
3

I had the same problem with activating an accordion with #id. Sadly I hadn't found a way to this, so I've created a hack. I iterate through div elements in my accordion in order to get the index of interesting div. It looks like this:

acc = 'tab-you-are-interested-in';

// find corresponding accordion
act = 0;
panels = $('#accordion-element > div');
for (i=0; i<panels.length; i++) {
    if ( panels[i].id == acc ) {
        act = i;  
    }
}

$('#accordion-element').accordion('activate', act);
Juarez answered 24/9, 2010 at 10:55 Comment(1)
just what I was looking for this moment!Darksome
H
2

When you click on header, it is h3 element and it opens the next div..that is functionality. Now, For activate , you need to provide index or the element. index might be different than your id. so i would use :

$('.selector').accordion('option', 'activate', $(h3#id));

If you have index, you can use that..But most of the cases , if you created accordion dynamically, it is not easy to get index of an id. You can find indices like this..

 var processingHeaders = $('#accordion h3');
 for (i = 0; i < processingHeaders.length; i++) {

        ids.push($(processingHeaders[i]).attr('id'));
        idsForLaterChecks.push($(processingHeaders[i]).attr('id')); 
    }

now i got ids.. using indexOf : find the index in the array and use it..

Note: // idsForLaterChecks is global

Heligoland answered 11/7, 2012 at 17:32 Comment(0)
S
1

With jQuery 1.9+ the:

$('#accordion').activate('activate', elementSelector);

is now:

$('#accordion').activate('option', 'active', elementSelector);

If you find it easier to use traversing methods, if you have HTML like this:

<div id="accordion">
    <h3><a href="#">Section</a></h3>
    <div>
        <p id="#item117" class="item">
            <a class="link-active" href="">Item 117</a>
        </p>            
    </div>
</div>

try this:

var myh3 = $('#item117').parent().prev('h3');
$('#accordion').accordion('option', 'active', myh3);
Selfmade answered 4/6, 2013 at 19:34 Comment(0)
C
0

Try

$('#accordion').activate('#item117');

or

$('#accordion').activate(document.getElementById('item117'));

The correct syntax for activating an accordion is

$(".selector").activate(var index)

where index is String,Element,boolean,Number,JQuery

Coon answered 14/12, 2009 at 7:3 Comment(0)
O
0

You can also enable and disable the accordion like this:

// Add the class ui-state-disabled to the headers that you want disabled
$( ".whatyouwantdisabled" ).addClass("ui-state-disabled");

To reactivate the tab:

// Remove the class ui-state-disabled to the headers that you want to enable
$( ".whatyouwantenabled" ).removeClass("ui-state-disabled");
Oneal answered 29/7, 2011 at 23:34 Comment(1)
This does not work for me. The classes are add, however, accordion does not react.Rhinology
G
0

Here's another way.

Include an ID="someId" in just each of the H3 header tags of the accordion and name the id's unique.

For example this id would be in series 'AccjA' the next h4 would be 'AccjB':

<h4 class="Accj" id="AccjA">
       <a href="#settings">A Fan?</a>
</h4>

Then activate whichever panel you wish with:

    $('#Accjoin').accordion('activate', '#AccjoinA')

I've used the above on a timeout to catch the attention of the user after the page is loaded with a 2 second delay using Ben Alman's ".doTimeout" function like:

$.doTimeout(2000, function () {
    $('#Accjoin').accordion('activate', '#AccjoinA')
});
Glair answered 26/8, 2012 at 8:50 Comment(1)
The above doesn't work now with 1.9. Like Jeremy Zerr points out now. jQuery 1.9.+ has a different setter!! api.jqueryui.com/accordion/#option-activeGlair
M
0

2024 solution.

Method 'activate' removed, instead of use 'option' method with option name 'active' (boolean or integer).

reference: https://api.jqueryui.com/accordion/#option-active

For example initialization accordion with active header class.

$( ".acc_selector" ).accordion({
    header: "h3", 
    collapsible: true, 
    active: (function(){
            var iact = 0;
            $('.header_selector').each(function(idx){
               if($(this).hasClass('header_selector_active')){
                  iact = idx;   
               }
            });
            return iact;
    })(),
    heightStyle: "content",
}); 
Momus answered 26/3 at 5:57 Comment(0)
U
-1

Whatch the jquery API

Activate the second content of the Accordion contained in .

$(".selector").activate(1)

Close all content parts of the accordion.

$(".selector").activate(false)

Activate the first element matching the given expression.

$(".selector").activate("a:first")
Unreal answered 10/10, 2012 at 0:21 Comment(0)
C
-2
$('#collapseOne').collapse('toggle');
Chromonema answered 30/12, 2015 at 1:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.