mrpatg
mrpatg

Reputation: 10117

javascript accordion menu that clicks once to expand, click twice to link

Im trying to get an accordion menu to behave the way I'd like it to, and it wont respond to my vocal pleas for compliance.

I desire to have the behave according to the following rules:

If there is no submenu, go to the link that has been set.

If there is a submenu, expand it, and setup the next step.

If there IS a submenu, and it HAS been expanded, a click on the heading will go to its respective link.

The accordion part works fine, but the 'once expanded, yet clicked again' link part does not. Any tips and/or pointers would be most appreciated.

The code i am using is below.

function initMenus() {
    $('ul#treeview ul').hide();
    $.each($('ul#treeview'), function(){
        $('#' + this.id + '.expandfirst ul:first').show();
    });
    $('ul#treeview 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();});

Upvotes: 1

Views: 912

Answers (1)

Jake
Jake

Reputation: 2470

Make sure your click event reaches the end of that function and does NOT return false when you have no sub-items to expand. The return false stops the href from being visited.

Upvotes: 1

Related Questions