JQuery - nested ul/li list, keep expanded after reload of page

Posted by H4mm3rHead on Stack Overflow See other posts from Stack Overflow or by H4mm3rHead
Published on 2010-04-19T19:55:14Z Indexed on 2010/04/19 20:03 UTC
Read the original article Hit count: 406

Filed under:
|
|

Hi, I have a nested ul/li list

<ul>
<li>first</li>
<li>second
<ul>
<li>Third</li>
</ul>
</li>
... and so on

I found this JQuery on the interweb to use as inspiration, but how to keep the one item i expanded open after the page has reloaded?

<script type="text/javascript">
        $(document).ready(function() {
            $('div#sideNav li li > ul').hide();    //hide all nested ul's
            $('div#sideNav li > ul li a[class=current]').parents('ul').show().prev('a').addClass('accordionExpanded');  //show the ul if it has a current link in it (current page/section should be shown expanded)
            $('div#sideNav li:has(ul)').addClass('accordion');  //so we can style plus/minus icons
            $('div#sideNav li:has(ul) > a').click(function() {
                $(this).toggleClass('accordionExpanded'); //for CSS bgimage, but only on first a (sub li>a's don't need the class)
                $(this).next('ul').slideToggle('fast');
                $(this).parent().siblings('li').children('ul:visible').slideUp('fast')
                .parent('li').find('a').removeClass('accordionExpanded');
                return true;
            });
        });
    </script>

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about html