My vertical nav works except for that when I press on the sibling drop down the previous opened drop down does not collapse. I was wondering if you could help me with this.
Here is my javascript code and if you want to look at it in full here is the jsfiddle link: http://jsfiddle.net/CLNBn/2/
$('.vertical-nav ul li:has("div")').find('div').hide();
$('.vertical-nav li:has("div")').find('span:first').click(function() {
$(this).parent('li').find('span:first').toggleClass("closed opened");
if ($(this).parent('li').find('span:first').attr('class') == 'closed') {
$(this).parent('li').find('span:first').text('+');
} else {
$(this).parent('li').find('span:first').text('-');
}
$(this).parent('li').find('div:first').slideToggle();
});
Since you already gave your elements reasonable classes, this task is quite easy. What you want is to find all
spanelements with classopenedthat are children of the siblings of the currentlielement, and trigger theirclickevent handler.This can be done with:
DEMO
I also took the liberty to simplify your code to:
As I said in my comment,
thisalready refers to thespanelement, there is no need to traverse up the DOM tree and search for it again.