bobmcleod (1) [Avatar] Offline
#1
I'm working with the show/hide menu from chapter 5, and have it working for site navigation. But I'm weak on the proper use of conditional logic in jQuery, so maybe someone can put me on the right track for the following problem.
I want the default state of a section to vary,ie to start submenu items hidden in cases where the user-selected item has no sub-menu items, but to start visible if the main li or one of its child items is assigned a currentpage class. The code as it now stands is as follows:

$(function(){
$('li:has(ul)')
.click(function(event){
if (this == event.target || $(event.target).parent()[0] == this) {
if ($(this).children('ul').is(':hidden')) {
$(this)
.css('list-style-image','url(minus.gif)')
.children('ul').slideDown();
}
else {
$(this)
.css('list-style-image','url(plus.gif)')
.children('ul').slideUp();
}
}
})
.css({cursor:'pointer',
'list-style-image':'url(plus.gif)'})
.children('ul').hide();
$('li:not(:has(ul))').css({
cursor: 'default',
'list-style-image':'none'
});
});

I feel a right simpleton for asking this, but I'm stumped, and grateful for any pointers.
mraj (104) [Avatar] Offline
#2
Re: chapter 5 menu conditional logic for currentpage
Hi Bob,
To re-show (unhide) a ul that contains an li with a 'currentpage' class, add this to the bottom of your jQuery code:
$('.currentpage').closest('ul').show(); // requires jQuery 1.3
$('.currentpage').parents('ul').show(); // use this instead for earlier versions

To re-show (unhide) the main li with 'currentpage', add this:
$('.currentpage').children('ul').show();