Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,

Please help?

I have a JQuery accordion, and would like to programmatically open a specific tab based on the ID of the tab. I feel that I need to get the index(?) of the div with the required ID in the accordion, and then call

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


The problem however is, how do I get the correct number of the div in the accordion, as I understand from the API that I can open accordion tabs based on its 0-index numbering?

example:

HTML
<div id="accordion">
<h3>Pretoria info</h3>
<div id="pretoria">some text</div>
<h3>Johannesburg Info</h3>
<div id="johannesburg">some text</div>
</div>



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



Thank you in advance!
Posted
Updated 17-May-13 8:45am
v2

1 solution

The set of your children will be
JavaScript
parent = $("#accordion");
children = parent.children();


Please see: http://api.jquery.com/children/[^].

Do with then whatever you want. For example you can always enumerate them:
JavaScript
children.each(function () {
   // do something with each child
});


Note that you can, for example, add any property to each object, such as "ordinal value":
C#
order = 0;
children.each(function () {
   this["Order"] = order;
   ++order;
});


Please see: http://api.jquery.com/jQuery.each/[^].

—SA
 
Share this answer
 
v2
Comments
Michelle Phoenix 17-May-13 15:46pm    
Thanx Sergey, it worked beautifully!
Sergey Alexandrovich Kryukov 17-May-13 16:16pm    
Great. Good luck, call again.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900