treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Need help with JQuery accordion menu...

  • Hello,

    I have this accordion menu script written for JQuery implemented on my site:

    function initMenus() {
    $('ul.menu ul').hide();
    $.each($('ul.menu'), function(){
    $('#' + this.id + '.expandfirst ul:first').show();
    });
    $('ul.menu li a').click(
    function() {
    var checkElement = $(this).next();
    var parent = this.parentNode.parentNode.id;

    if($('#' + parent).hasClass('noaccordion')) {
    $(this).next().slideToggle('normal');
    return false;
    }
    if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
    if($('#' + parent).hasClass('collapsible')) {
    $('#' + parent + ' ul:visible').slideUp('normal');
    }
    return false;
    }
    if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
    $('#' + parent + ' ul:visible').slideUp('normal');
    checkElement.slideDown('normal');
    return false;
    }
    }
    );
    }
    $(document).ready(function() {initMenus();});


    It works very well. However, I would like to add some functionality to it. Namely, have it save the menu state between pages. I am guessing I would just tell it to add a class to the current menu, or modify this line somehow:

    $.each($('ul.menu'), function(){
    $('#' + this.id + '.expandfirst ul:first').show();
    });


    I don't really know how to implement this, so any help would be appreciated! Thanks!