Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-27-2004, 11:49 AM   PM User | #1
alexhob
New to the CF scene

 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
alexhob is an unknown quantity at this point
using a function to hide or show layers

I am new to JS and am struggling with a function I found on the net that shows or hides a layer. The function has the name of the layer hard coded into it. However I want to use the function by several links and so want to pass the layer name as a parameter. Can anyone show me how to do this?

Thanks.

function showorhide(v){
if(v){
visi="visible";
}
else{
visi="hidden";
}

//NN4
if(document.layers){

document.fred.visibility=visi;
}

//IE
if(document.all){

document.all.fred.style.visibility=visi;
}

//NN6
if(document.getElementById){

document.getElementById("fred").style.visibility=visi;
}
}

<div id="fred" style="position:relative;left:10;top:0;background-color:#FF0000;width:100; visibility:hidden;font-size:16px;color:white;font-weight:600">Hi, I'm fred</div>
<CENTER><A class="bodylink" id="lshow" href="javascript:void(0)" onClick="showorhide(1, fred);">show layer</A> | <A class="bodylink" href="javascript:void(0)" onClick="showorhide(0);">hide layer</A></CENTER>
alexhob is offline   Reply With Quote
Old 04-27-2004, 12:06 PM   PM User | #2
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
Code:
function showorhide(v,layerId){
  if(v){
    visi="visible";
  }
  else{
    visi="hidden";
  }

  //NN6+/IE5+ and other modern browsers
  if(document.getElementById){
    document.getElementById(layerId).style.visibility=visi;
  }
  //IE4+
  else if(document.all){
    document.all[layerId].style.visibility=visi;
  }
  //NN4
  else if(document.layers){
    document.layers[layerId].visibility=visi;
  } 
} 
...
<A class="bodylink" href="#" onClick="showorhide(0,'fred');return false">hide layer</A>
I re-ordered the if-statements and put else-statements for efficiency.
Since IE5+/NN6+ are the prevalent browsers, you should check for that first.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Old 04-27-2004, 12:15 PM   PM User | #3
alexhob
New to the CF scene

 
Join Date: Feb 2004
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
alexhob is an unknown quantity at this point
Thanks a lot.

Can you explain why the parameter name has to be in either () or [] and when to chose/where to apply either?

Thanks.
alexhob is offline   Reply With Quote
Old 04-27-2004, 12:25 PM   PM User | #4
glenngv
Supreme Master coder!


 
glenngv's Avatar
 
Join Date: Jun 2002
Location: Los Angeles, CA Original Location: Philippines
Posts: 10,241
Thanks: 0
Thanked 112 Times in 111 Posts
glenngv will become famous soon enough
You use () for methods/functions and [] for collections/arrays.
__________________
Glenn
_____________________________________________
Play Tower of Hanoi Android app (Ad-FREE!)
Play Tower of Hanoi Android app (FREE!)
Go to Tower of Hanoi Leaderboard
Play Tower of Hanoi Facebook app
glenngv is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:41 PM.


Advertisement
Log in to turn off these ads.