views:

66

answers:

2

What I'm trying to do is give my user 4 options (simple links). If the click one of these, a div below is shown based on their selection. The issue I'm having is figuring out how to hide the div below if another top link is clicked. Right now I'm able to show all 4 sub-divs at the same time, where it should hide the currently visible one, then show the new one.

Any help?

Thanks!

A: 

This assumes all children of container_id are divs:

function show_item( container_id, item_id ) {
allItems = $(container_id).getElementsBySelector( 'div' );
$A(allItems).each(Element.hide);
$(item_id).show();
}

Thanks! Bear with me, as I'm new to JS - so if my 4 sub divs are id="a1", id="a2", id="a3", and id="a4" - do I need 4 functions based on the code above but switching out container_id with the appropriate id I want to show??
<div id="top"><div id="a1"></div><div id="a2"></div><div id="a3"></div><div id="a4"></div></div>To show a1 do:show_item( 'top', 'a2' );
I meant: to show a1 do : show_item( 'top', 'a1' ) !!!
A: 
$$('.sub-selection').each(Element.hide);
$('#selected_element').show();
Duncan Beevers
think you may have added some jquery in the second code sample, should be: $('selected_element').show(); first one could also be rewritten as: $$('.sub-selection').invoke("hide");
seengee