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 03-20-2010, 07:55 PM   PM User | #1
popapez
New to the CF scene

 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
popapez is an unknown quantity at this point
boolean function to hide/show div

Hi, I am very new to javascript and html.

I am trying to create a function which will hide or show a div (div1). Right now, it is always showing the div whether the boolean value is true or false. I know that it is getting inside the else if part, but it doesn't seem to be working.


Code:
<script type="text/javascript">

function hideDiv(flag)
	{
	if (flag == false)
		{
		document.getElementById('div1').style.visibility="visible";
		}
	else if (flag == true)
		{
		document.getElementById(div1).style.visibility="hidden";
		}	
	}

var bool = new Boolean(true)
hideDiv(bool);
</script>

<body>
<div id="div1">
Text!
</div>
</body>
Thank you for taking a look
popapez is offline   Reply With Quote
Old 03-20-2010, 08:04 PM   PM User | #2
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
document.getElementById(div1).style.visibility="hidden";


Here you are:-

Code:
<script type="text/javascript">

function hideDiv(flag) {
if (flag == false) {  // or if (!flag) {
document.getElementById("div1").style.visibility="visible";
}
else if (flag == true) {  // or just else, or else if (flag) {
document.getElementById("div1").style.visibility="hidden";
}	
}

</script>

<body onload = "hideDiv(false)">

<div id="div1">
Text!
</div>

There is no point in creating the variable bool.


Quizmaster: On what part of the body is a lobotomy performed?
Contestant: The bottom.

Last edited by Philip M; 03-20-2010 at 08:10 PM..
Philip M is offline   Reply With Quote
Old 03-20-2010, 08:12 PM   PM User | #3
popapez
New to the CF scene

 
Join Date: Mar 2010
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
popapez is an unknown quantity at this point
Thank you very much, works great!
popapez is offline   Reply With Quote
Old 03-20-2010, 11:19 PM   PM User | #4
randomuser773
Banned

 
Join Date: Nov 2008
Location: not found
Posts: 284
Thanks: 0
Thanked 53 Times in 51 Posts
randomuser773 can only hope to improve
Quote:
Originally Posted by popapez View Post
Thank you very much, works great!
Wouldn't that function be more useful if it could be used on any element?
randomuser773 is offline   Reply With Quote
Old 03-20-2010, 11:52 PM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,765
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
Lightbulb Consider this ...

Why not make it general purpose so that you might use it here or elsewhere?
Code:
<script type="text/javascript">
function hideDiv(IDS,flag) {
  var sel = document.getElementById(IDS);
  if (flag == false) { sel.style.visibility="visible"; }  // or sel.style.display="block";
                else { sel.style.visibility="hidden"; }	  // or sel.style.display="none";
}
</script>

<body onload = "hideDiv('div1',false);hideDiv('div2',true)">

<div id="div1">Text!</div>
<div id="div2">More Text!</div>
jmrker 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 03:46 AM.


Advertisement
Log in to turn off these ads.