Forum Moderators: open
in between your <head> tags put this java script in
<script language=javascript type='text/javascript'>
function hideDiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideShow').style.visibility = 'hidden';
}
else {
if (document.layers) { // Netscape 4
document.hideShow.visibility = 'hidden';
}
else { // IE 4
document.all.hideShow.style.visibility = 'hidden';
}
}
}
function showDiv() {
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById('hideShow').style.visibility = 'visible';
}
else {
if (document.layers) { // Netscape 4
document.hideShow.visibility = 'visible';
}
else { // IE 4
document.all.hideShow.style.visibility = 'visible';
}
}
}
</script>
As you can see it gets the div ID for each of the browsers and hides it or shows it. I named the Div Id hideShow to make it easy for you.
Then in your document put in your Div element the id name hideShow you can change this of course
<div id="hideShow" ..etc>
My content
</div>
and this to call the javascript to hide it
<a href="javascript:hideDiv()">Hide Div</a>
and this to show it
<a href="javascript:showDiv()">show Div</a>
That simple
regards,
Mark
<edit> syntax error </edit>
Thanks for the codes! However, when I tried to add more layers <div>, it doesn't work. The 'showdiv' and 'hidediv' shows and hides all the layers respectively.
<div id="256MB"><img src=xxx> $71</div>
<div id="512MB"><img src=yyy> $167</div>
<div id="1GB"><img src=zzz> $333</div>
Actually, my goal is to have several text links that can display/overwrite the layer in the same location.
Visual example...
256MB ¦ 512MB ¦ 1GB
-----------------------
[PIC] [PRICE] <-- want this part to change to their respective layers when clicked on '256MB' '512MB' & '1GB' links.
-----------------------
Any ideas on how to do this? Any help would be much appreciated.
<script language="javascript">
<!--var state = 'none';
function showhide(layer_ref) {
if (state == 'block') {
state = 'none';
}
else {
state = 'block';
}
if (document.all) { //IS IE 4 or 5 (or 6 beta)
eval( "document.all." + layer_ref + ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
hza = document.getElementById(layer_ref);
hza.style.display = state;
}
}
//-->
</script>
and this html code for each division that is to be hidden/shown:
<p><a href="#" onclick="showhide('div1');">Show/hide me</a></p>
<div id="div1" style="display: none;">This is the content</div>
The difference between using the css tags 'visibility' and 'block' is that with 'block' the layout will be updated as well.
<script language=javascript type='text/javascript'>
function hideDiv(pass) {
var divs = document.getElementsByTagName('div');
for(i=0;i<divs.length;i++){
if(divs[i].id.match(pass)){//if they are 'see' divs
if (document.getElementById) // DOM3 = IE5, NS6
divs[i].style.visibility="hidden";// show/hide
else
if (document.layers) // Netscape 4
document.layers[divs[i]].display = 'hidden';
else // IE 4
document.all.hideShow.divs[i].visibility = 'hidden';
}
}
}
function showDiv(pass) {
var divs = document.getElementsByTagName('div');
for(i=0;i<divs.length;i++){
if(divs[i].id.match(pass)){
if (document.getElementById)
divs[i].style.visibility="visible";
else
if (document.layers) // Netscape 4
document.layers[divs[i]].display = 'visible';
else // IE 4
document.all.hideShow.divs[i].visibility = 'visible';
}
}
}
</script>
The document.getElementsByTagName gets the div elements and puts them in an array. Then it sets to visble depending on your div id that you pass to it. I dont think document.getElementsByTagName works in older browsers.
Name your DIVS like so, or whatever you like
<div id="256a">
My content
</div>
<div id="256b">
Blah
</div>
<div id="512a">
Blah
</div>
To hide or show notice only the first few characters are needed.
<a href="javascript:hideDiv('256')">Hide Div 256</a>
<a href="javascript:showDiv('256')">Show Div 256</a>
<a href="javascript:hideDiv('512')">Hide Div 512</a>
<a href="javascript:showDiv('512')">Show Div 512</a>
Not as simple but not too hard. Hope this helps.
regards,
Mark
But I have two more questions...
1. How can I hide all layers upon loading the page?
2. The layer would not go on top of the other layer. I would like the layers to be displayed in the same area. What I'm doing is sorta like a folder thing. One folder would go on top of another when one clicks on a text link.
The result I'm getting now is layers extending down the page.
Any ideas?
ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens ("-"), underscores ("_"), colons (":"), and periods (".").W3C Reference Page [w3.org]
<script language=javascript type='text/javascript'>
function showDiv(pass) {
var divs = document.getElementsByTagName('div');
for(i=0;i<divs.length;i++){
if(divs[i].id.match(pass)){//if they are 'see' divs
if (document.getElementById) // DOM3 = IE5, NS6
divs[i].style.visibility="visible";// show/hide
else
if (document.layers) // Netscape 4
document.layers[divs[i]].display = 'visible';
else // IE 4
document.all.divs[i].visibility = 'visible';
} else {
if (document.getElementById)
divs[i].style.visibility="hidden";
else
if (document.layers) // Netscape 4
document.divs[i].visibility = 'hidden';
else // IE 4
document.all.divs[i].visibility = 'hidden';
}
}
}
</script>
This script will show the div you pass to it and hide the rest.
Your links to hide show div
<a href="javascript:showDiv('F256')">show 256 Div hide rest</a>
<a href="javascript:showDiv('F512')"> show 512 Div hide rest</a>
Your Divs with abosulte positioning and required other properties set to what you like of course.
<div id="F2561" style="position: absolute; left:5px; top:54px; background-color: #0093DD; border: 1px none #000000; visibility: hidden">
256 Here </div>
<div id="F512" style="position: absolute; left:5px; top:54px; background-color: #0093DD; border: 1px none #000000" >
512 Here
</div>
You can see that the 256 is set to invisible, wont be shown until the link is pressed to show it.
regards,
Mark
<edit>Syntax Error</edit>
[webmasterworld.com...]
---END---
ok so that it! hope that helps others cuz it sure helped me! peace out
<script>
function showhide(id){
if (document.getElementById){
obj = document.getElementById(id);
if (obj.style.display == "none"){
obj.style.display = "";
} else {
obj.style.display = "none";
}
}
}
</script>
Just trying to do the show/hide html thingie, but was trying to do it without js - is it possible ?
var state = 'none';
function showhide(layer_ref){
if(state == 'block'){
state = 'none';
}else{
state = 'block';
}
if(document.all){ //IS IE 4 or 5 (or 6 beta)
eval("document.all." +layer_ref+ ".style.display = state");
}
if (document.layers) { //IS NETSCAPE 4 or below
document.layers[layer_ref].display = state;
}
if (document.getElementById &&!document.all) {
hza = document.getElementById(layer_ref);
hza.style.display = state;
}
}
I'm no javacscript expert, is there a way to modify the code so that the content of my div starts hidden?
It would be handy to choose the original state for different divs on a page.. eg.
onclick="showhide('advanced','starthidden');"
or
onclick="showhide('advanced','startvisible');"
Hope you can help.
Thanks
Seamless
I've tried using the code that waldemar posted and it works. I like its use and functionality. However, users have to double click to call the function unless you set the function to load in the body onload. That's fine.
Is there a way to set a cookie so the preference of any div is saved for the session or indefintely?
I can clarify if this doesn't make sense.
Andrew