views:

916

answers:

2
function showlayer(layer){
var myLayer = document.getElementById(layer).style.display;
if(myLayer=="none"){
document.getElementById(layer).style.display="block";
} else { 
document.getElementById(layer).style.display="none";
}
}

I need this code to close the current layer and them open another. These layers exist in the content div and are nested 12 deep.

For instance:

This is in the body of the container(navigation) to control the content container which is nested 12 deep. (I came up with an idea on my own but it wound up closing all layers making my web page disappear)

<li><a href="#" onclick="javascript:showlayer('USNews')">US News</a></li>

Hence when the navigation button marked US News is clicked via the above it opens

Now, if I have US News open, and I click on say Politics (the third nested layer, I want USNews (the first nest layer) to close and only Politics to open (noticing of course that Politics is the Third Layer and USNews is the first layer).. and so forth..

I've attempted if else statements but I have been out of this for years now and am just frustrated beyond belief... any help would be greatly appreciated

A: 

Instead of writing the raw Javascript, why not use a library instead.

In JQuery you could move to the correct layer, and hide or show it relatively easily.
(Probably one line of script tbh.)

Bravax
+1  A: 

You can loop all the layers and hide them before. And only then show selected one.

function showlayer(layer){

var Layers=document.getElementsByTagName("div");
for(i=0;i<Layers.length;i++){
    if(Layers.getAttribute("class")=="layer"){
    Layers.style.display="none";
    }
}
document.getElementById(layer).style.display="block";
}


<li onclick="javascript:showlayer('USNews')"><a href="#" >US News</a></li>
<li onclick="javascript:showlayer('UkNews')"><a href="#" >Uk News</a></li>
<li onclick="javascript:showlayer('ArNews')"><a href="#" >Ar News</a></li>

<div id="USNews" class="layer"></div>
<div id="UkNews" class="layer"></div>
<div id="ArNews" class="layer"></div>
Eldar Djafarov
I may not be following this post correctly - I've been awake now for nearly two days rushing to get this thing done only to find out there are now carrier problems besides that...Are you stating the "function showlayer(layer) needs to be compiled or "built first" (comiled for us old timers)?When I attempted to runt he script on a new page nothing but the above, I am getting (now mind you line 12 is the line "if" statement) an error:Line 12 char 5 (if is idented 5 characters)Error Object doesn't support this propert or methodCode 0changed it to ("class" == "layer")
This responds with the error:Line 24 Char 1Error Object expected (of course in the line below<li onclick="javascript:showlayer('USNews')"><a href="#" >US News</a></li>So I attempted:<li> <a href="#"onclick="javascript:showlayer('USNews')"> US News</a></li> No go,It stated the same as the second error execpt referecing the third onclick line of codeSo I switched the original script back to ("class")=="layer:) and the error pointed back to this line... I'll keep tinkering...
However, I really do appriecate the help... I am no javascript pro and have toyed with web pages in almost 5 years... Network Engineer by trade... (p.s. please excuse typo's on my part really tired)Thanks again -- will go with the Ant maybe for I downloaded the Google API toolkit a few days ago however I am having problems (permission problems) running ANT or Apache (I can't remember at this time) on my Server 2008 -- I'll try installing it on my Vista virtual client and see what happens
I need sleep can't edit aboveThe original script relfects an error on the line:if(Layers.getAttribute("class")=="layer"){Line 12 char 5 Error Object doesn't support this property or method Code 0I changed to("class"=="layer"))THen the error occured in the line:<li onclick="javascript:showlayer('USNews')"><a href="#" >US News</a></li>"Line 24 Char 1 Error Object expected "And then I changed that to:<li> <a href="#"onclick="javascript:showlayer('USNews')"> US News</a></li>Wait let me try putting the "<li>'s in a different layer...I'll let you know what happens