views:

804

answers:

1

I have an ccordion menu, and each menu header has a little arrow img that changes when the menu slides down and up.

This is the code so far:

$(document).ready(function() {
$("div.menu_body").hide();
$("#menuheader div.menu_head div.detailsPanel").toggle(function() {

    $(this).addClass("detailsPanelSelected").parent(this).next("div.menu_body").slideToggle(300).siblings("div.menu_body").slideUp("slow");
},

function() {

$(this).removeClass("detailsPanelSelected").parent(this).next("div.menu_body").slideUp("slow");

} );

This works fine when I toggle one menu item at a time.

But say I expand one menu item, and the click on another menu header. What happens is that the first menu item slides up and the newly clicked slides down, just like expected. But the arrow gif doesn't change back on the menu that slides up. This happens because it's toggle function is still in the first state, and if I click on it again, thus removing the "detailsPanelSelected" class, all that happens is that the image changes back. Looking at the code, this is expected.

So my question is, how do I make it so that a menu items "detailsPanelSelected" is removed when another menu header is clicked? Can I somehow reset the pending toggle function?

A: 

try something like this:

$("#menuheader div.menu_head div.detailsPanel").toggle(function() {

$(".detailsPanelSelected").each(function(i){ this.removeClass("detailPanelSelected"); })

$(this).addClass("detailsPanelSelected").parent(this).next("div.menu_body").slideToggle(300).siblings("div.menu_body").slideUp("slow");

}

the important part of the code is this:

$(".detailsPanelSelected").each(function(i){ this.removeClass("detailPanelSelected"); })

it should be placed above the code in which you add the class that changes the arrow's direction... therefore all of the elements are back to their closed state, then your next function "opens" the correct one

jhensley2
This doesn't work. What it does is that it opens the menu body and then you have to close it again before you can open any of the other menu bodies. The arrow image changes though.When a menu body slides down, the menu_body div gets a "display:block" attr. This could be a way of identifying the last open menu body. I have to manipulate the last opened menu items arrow img, regardless of which of the other menu items is clicked. I've had no luck so far...
Soeren
This is the html for a menu item:<div id="menuheader" class="menu_list"> <div class="menu_head"> <img class="Pic" alt="Some Picture" src="Some Picture.jpg" /> <div class="detailsPanel"> <span class="font1">Some text</span> </div> <div class="TestDiv"> <span class="font1">Some text</span> </div> </div> <div class="menu_body"> <div class="body_divs_container"> ..... ..... ..... ..... </div> </div></div>
Soeren
Uhm, not many options for leaving code in the comments field, huh?
Soeren
is the menu icon being set correctly for ONLY the selected menu item?
jhensley2
Yes, it's a background image on the <div class="detailsPanel"></div> The "detailspanel" class has a background image with the "closed" arrow, and the "detailsPanelSelected" class, which is being added to the div when it is clicked, sets the image with the "open" arrow.
Soeren