views:

126

answers:

1

I am trying to create a multilevel accordion using nested unordered list.

What I am trying to accomplish:

  • Show only the "active" ul (other remain hidden)

  • When there is a nested ul - toggle only the nested ul (2222 -> 2.1)

Full code here: http://pastie.org/852421

JS

$(document).ready(function() {

    $('ul.menu li ul').hide();

    $('ul.menu li a').click(function(e){

        $('ul.menu li a').each(function(i){
            if($(this).next().is("ul") && $(this).next().is(":visible")){
                $(this).next().slideUp("fast");
            }
        });

        var e = $(e.target);                

        if(e.next().is("ul") && e.next().is(":visible")){
            e.next().slideUp("fast");
            } else {
            e.next().slideDown("fast");
        }
    });

});

HTML

<ul class="menu">
    <li><a href="#">11111</a>
        <ul>
            <li><a href="#" id="one">1.1</a></li>
            <li><a href="#" id="one">1.2</a></li>
        </ul>
    </li>
    <li><a href="#">22222</a>
        <ul>
            <li><a href="#">2.1+ (problem)</a>
                <ul>
                    <li><a href="#">2.1.1</a></li>
                    <li><a href="#">2.2.2</a></li>
                </ul>
            </li>
            <li><a href="#">2.2</a></li>
        </ul>
    </li>
    <li><a href="#">33333</a>
        <ul>
            <li><a href="#">3.1</a></li>
            <li><a href="#">3.2</a></li>
        </ul>
    </li>
</ul>
+1  A: 

just add class .active to active li and then call with jQuery:

$("ul:has(li.active)").slideDown();

simple ;]

Edit:
also, I previously used following:

$(rootparent+' ul').hide();
$(rootparent+' ul:has(li#active)').show();
$(rootparent+">li:has(a[href*='"+activepage+"'])>ul").show();

(rootparent is topmost ul, activepage is active URI)

Adam Kiss
This doesn't solve any of my problems.
Eeyore
ah, little mistake.
Adam Kiss
and don't be so quick with -1
Adam Kiss
this gave me few ideas but I still can't get it to work.
Eeyore
then by no means post new code.
Adam Kiss