1

I'm using the code below to toggle an accordion menu, but each time an item in the child menu is clicked, the menu slides up without going to the actual link.

http://jsfiddle.net/Y2vM3/

When clicking MISC in the jsfiddle above, the menu opens. But when clicking the child link, it closes right back up. It works fine when "return false;" is removed (it's there to prevent the browser from jumping to the top of the page), but with it, the sub-menu just slides up..?

jQuery(document).ready(function ($) {
    jQuery('.menu ul').slideUp(0);

    jQuery('.menu li.sub').click(function () {
        var target = jQuery(this).children('a');
        if(target.hasClass('menu-expanded')){
            target.removeClass('menu-expanded');
        }else{
            jQuery('.menu-item > a').removeClass('menu-expanded');
            target.addClass('menu-expanded');
        }
        jQuery(this).find('ul:first')
            .slideToggle(350)
            .end()
            .siblings('li')
            .find('ul')
            .slideUp(350);
        return false;
    });
});
Ray
  • 171
  • 10

1 Answers1

0

By using return false; you're telling the browser to not follow the link. Use preventDefault instead. Here's more on the distinction.

http://jsfiddle.net/isherwood/Y2vM3/4/

...
        jQuery(this).find('ul:first')
            .slideToggle(350)
            .end()
            .siblings('li')
            .find('ul')
            .slideUp(350);
        preventDefault();
    });
});

Also, use

href="javascript:void(0)"

instead of a hash.

Community
  • 1
  • 1
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • When "MISC" is clicked, the browser now jumps to the top of the page :( – Ray Aug 08 '13 at 13:22
  • I'm using WP so using "javascript:void(0)" as link won't stick, but leaving the link empty does. Cheers! – Ray Aug 08 '13 at 13:28