Forum Moderators: open

Message Too Old, No Replies

Need to use hide/show multiple times

         

orion8083

3:45 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



The below quote was posted elsewhere on these forums by Bernard Marx. He mentioned that it could be amended slightly to allow for more than on set of hide/show sections. This code is working perfectly except for this one thing. I don't know javascript, but I did try to edit the code by changing the variables and function names in an attempt to have one set of divs toggle independently of the other.

This is the only thing that should show when the page initially loads. Basic layout is:
Category1 -- Category2 -- Category3

When you click on a category, a Selection pops up/ these selections are inside named according to the category. So if Category1 was selected, the selections would be named Category1_Selection1, Category1_Selection2, etc.
Selection1 -- Selection2 -- Selection3 -- Selection4

After choosing a selection, all information relative to the category and selection pop up below.

The Selections are currently toggling fine. If I try to do the same thing with the Categories, they will also toggle the selections off. Is there a way around this?

Here's the basic toggler. Sweeten according to taste.

If you want more than one set on a page, we need to amend it (but only slightly).

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>TEST</title>
<style type="text/css">
.text{display: none;width:300px;border: solid 1px lightblue;}
#text0{display: block;}
</style>
<script type="text/javascript">
/*
Global "swap" holder
Use value, null, if no layer initially visible
*/
var currLayerId = "text0";

function toglayer(id)
{
if(currLayerId) setDisplay(currLayerId, "none");
if(id)setDisplay(id, "block");
currLayerId = id;
}

function setDisplay(id,value)
{
var elm = document.getElementById(id);
elm.style.display = value;
}
</script>
</head>
<body>
<!--buttons-->
<a href="#" onclick="toglayer('text0');return false;">text0</a>
<a href="#" onclick="toglayer('text1');return false;">text1</a>
<a href="#" onclick="toglayer('text2');return false;">text2</a>
<a href="#" onclick="toglayer(null)">Show none</a>
<!--layers-->
<div id="text0" class="text">Content of text0</div>
<div id="text1" class="text">Content of text1</div>
<div id="text2" class="text">Content of text2</div>
</body>
</html>

orion8083

11:41 pm on Mar 2, 2009 (gmt 0)

10+ Year Member



Answered my own question. I also had an issue with the onmouseout function, so I created a quick hide on mouseout function.

I feel smart...woot.


var currLayerIdtwo = "";

function togLayertwo(idtwo)
{
if(currLayerIdtwo) setDisplaytwo(currLayerIdtwo, "none");
if(idtwo)setDisplaytwo(idtwo, "block");
currLayerIdtwo = idtwo;
}

function setDisplaytwo(idtwo,valuetwo)
{
var elm = document.getElementById(idtwo);
elm.style.display = valuetwo;
}

function hideDisplay(id)
{
if(id)setDisplay(id, "none");
}

daveVk

1:15 am on Mar 3, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html><head><title>TEST</title>
<style type="text/css">
.text{display: none;width:300px;border: solid 1px lightblue;}
#text0{display: block;}
</style>
<script type="text/javascript">
/*
Global "swap" holder
Use value, null, if no layer initially visible
*/
var currLayerId = ["text0", "other0"];

function toglayer(id,n)
{
if(currLayerId) setDisplay(currLayerId[n], "none");
if(id)setDisplay(id, "block");
currLayerId[n] = id;
}

function setDisplay(id,value)
{
var elm = document.getElementById(id);
elm.style.display = value;
}
</script>
</head>
<body>
<!--buttons-->
<a href="#" onclick="toglayer('text0',0);return false;">text0</a>
<a href="#" onclick="toglayer('text1',0);return false;">text1</a>
<a href="#" onclick="toglayer('text2',0);return false;">text2</a>
<a href="#" onclick="toglayer(null,0)">Show none</a>
<!--layers-->
<div id="text0" class="text">Content of text0</div>
<div id="text1" class="text">Content of text1</div>
<div id="text2" class="text">Content of text2</div>
</body>
</html>