views:

46

answers:

1

Hi there,

First question ever asked at stackoverflow. So, problem is: Two accordion declarations on document.ready (jquery 1.4.2 and jquery ui 1.8.2):

       $(document).ready(function () {
        $("#accordion").accordion({
            header: 'h3'
        });

        $("#accordion2").accordion({ 
            header: 'h4' 
        });

        $(function () {
            $(".get-index").click(function () {
                var activecontent = $("#accordion").accordion("option", "active");
                alert(activecontent);                   
            });
        });
    });

HTML:

<div id="accordion">
    <h3><a href="#">Section 1</a></h3>
    <div>
        Content Section 1: Parent
        <div id="accordion2">
            <h4><a href="#">SubSection 1</a></h4>
            <div>content section 1: child</div>
            <h4><a href="#">SubSection 2</a></h4>
            <div>content section 2: child</div>
            <h4><a href="#">SubSection 3</a></h4>
            <div>content section 3: child</div>
            <h4><a href="#">SubSection 4</a></h4>
            <div>content section 4: child</div>
        </div>
    </div>
    <h3><a href="#">Section 2</a></h3>
    <div>
        Content Section 2: Parent
    </div>
    <h3><a href="#">Section 3</a></h3>
    <div>
        Content Section 3: Parent
    </div>
    <h3><a href="#">Section 4</a></h3>
    <div>
        Content Section 4: Parent

        <button type="button" class="get-index ui-button ui-button-text-only ui-widget ui-state-default ui-corner-all">
            <span class="ui-button-text">index</span>
        </button>

    </div>
</div> 

And finally: what's wrong and why "activecontent" is 7? I know, that there are 4 parent panels + 4 child panels and starting from 0, it is 7. But I'm trying to get index of last parent panel and it should be 3.

Any help much appreciated.

Code posted: http://jsbin.com/eqewe

+1  A: 

Unfortunately this is a bug in jQuery UI, in the accordion code:

o.active = o.collapsible && clickedIsActive ? false 
  : $('.ui-accordion-header', this.element).index(clicked);

It's finding any $('.ui-accordion-header'), not just the header selector you specified and not only immediate children. I'll put this in as a bug with the jQuery UI guys, the .active property really should be set differently. I've entered a bug with the jQuery UI team for this here: http://dev.jqueryui.com/ticket/5841


You can work-around it for now by finding the element yourself with .index(), like this:

$(function () {
  $(".get-index").click(function () {
    var a= $("#accordion").children('.ui-state-active').index('#accordion > h3');
    alert(a);                   
  });
});​

You can try it out here

Nick Craver
Thanks a lot, question is answered.
@msqsf - Welcome :) Be sure to accept answers on this and future questions, it helps the next guy that comes along looking for the same issue. I'm entering that bug report now since this is a valid bug, I'll update this once I have a link.
Nick Craver