Forum Moderators: open

Message Too Old, No Replies

jquery accordion only show one element at a time

jquery java accordion toggle

         

generic

2:49 pm on Oct 15, 2009 (gmt 0)

10+ Year Member



I've got the following jqeury code running on my site and it works fine except it allows more than one element (in this case li) to show at a time. I need to modify it so that when one element is activated, the rest close. Any help would be greatly appreciated, thanks in advance!


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();});

lexipixel

5:43 am on Oct 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Maybe the actual jQuery UI accordion() function would work better...

[docs.jquery.com...]

lexipixel

5:43 am on Oct 17, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



(and will greatly simplify your code)