How to collapse another items

Hello,

this code for tree category, i want when i click on item close another open items
please help


function loadLevel(id)
{
	if ($('#cat_'+id).attr('class').split(' ').indexOf('working') < 0)
	{
		if ($('#cat_'+id).attr('class').split(' ').indexOf('expanded') < 0)
		{
			$('#cat_loading_'+id).fadeIn('fast');
			$('#cat_'+id).removeClass('cat_item_plus').addClass('cat_item_minus').addClass('working');
					var cc_chb_of = cc_selected == 0 ? true : false;
					var xfix = $('#crossed_'+id).attr('checked') ? 'crossed_'+id : false;
					xajax_getCatLevel(id, false, 'category_level_crossed.tpl', '', 'crossed', cc_chb_of, xfix);
					crossed_clear();
				}
				else
				{
					if ($('#parent_'+id).css('display') == 'none')
					{
						$('#parent_'+id).slideDown('slow');
						$('#cat_'+id).removeClass('cat_item_plus').addClass('cat_item_minus');
					}
					else
					{
						$('#parent_'+id).slideUp('normal');
						$('#cat_'+id).removeClass('cat_item_minus').addClass('cat_item_plus');
					}
				}
			}
		}

what error messages are you getting?

i didn’t get any error message, its work fine, but this code its for tree menu when i click on item one its open the tree and when i click on another item its still open tree one and open tree two, what i want on click on another item hide all tree and open tree what i clicked.

oh ok.

For me it’s a lot easier and I would use less code by doing it with plain javascript, rather than with jquery. Hopefully someone else will come long if you must use jquery.

The standard way to achieve this is to first close everything and then to open the one that was clicked.

Thanx for replay, so how to do this???

You can loop through all the elements first and set their display style to none and then set the display style of the clicked one to block.

Bad webdev, he’s using jQuery for his existing code so he might as well continue to benefit from the library that he’s currently using.

He can loop through all of the elements that have an id starting with “parent_”
In that loop he can check if the element has a matching id number that was initially passed in, so that different behaviour can be performed with those that match.

It might be something like this:


$('[id^="parent_"]').each(function () {
    if (this.id.split('_')[1] == id) {
        // show this element
    } else {
        // hide this element
    }
}