User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 456,452 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,623 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 953 | Replies: 7
Reply
Join Date: Sep 2007
Posts: 25
Reputation: roy-- is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
roy-- roy-- is offline Offline
Light Poster

Hiding empty <div>'s

  #1  
Jun 17th, 2008
Hello,

I've got a few <div>'s that are dynamically generated,

what I want to do is construct a conditional javascript statement that will set display:none to all empty <div class='policy_text'>

for example:

if { <div class='policy_text'></div> } then set div.policy_text to display:none;

elseif {

<div class='policy_text'>text contents.. bla bla</div>

} then do nothing

Thanks for your help!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 850
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 66
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Hiding empty <div>'s

  #2  
Jun 17th, 2008
<script language="javascript">
function toggledivs(class, content)
{
	var divs = document.getElementsByTagName('div');
	for (var i = 0; i < divs.length; i++)
	{
		var div = divs[i];
		if (div.className == class && trim(div.innerHTML) == content && div.style.display != "none")
		{
			div.style.display = "none";
		}
	}
}

function trim(string) {
	string = string.replace(/^\s+/,"");
	return string.replace(/\s+$/,"");
}
</script>
Last edited by R0bb0b : Jun 17th, 2008 at 8:00 am.
Reply With Quote  
Join Date: Sep 2007
Posts: 25
Reputation: roy-- is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
roy-- roy-- is offline Offline
Light Poster

Re: Hiding empty <div>'s

  #3  
Jun 17th, 2008
Thanks, trying to use the function:

toggledivs('policies', '');

keep getting errors though, am not sure i understand how to use the function
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 850
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 66
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Hiding empty <div>'s

  #4  
Jun 17th, 2008
Your going to use a function call like this
<a href="javascript:toggledivs('policy_text', '');">toggle divs</a>
the two parameters that are sent is:
param1: the name of the class to toggle
param2: the content of the div to match to(so it doesn't necessarily need to be empty, just match the second parameter.)
Last edited by R0bb0b : Jun 17th, 2008 at 10:34 am.
Reply With Quote  
Join Date: Sep 2007
Posts: 25
Reputation: roy-- is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
roy-- roy-- is offline Offline
Light Poster

Re: Hiding empty <div>'s

  #5  
Jun 17th, 2008
tried that, nothing happens in firefox, and internet explorer gives an object expected error ;/
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 850
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 66
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Hiding empty <div>'s

  #6  
Jun 17th, 2008
funny, works in ff for me just fine, but I do get an error in IE. Looking for a workaround.
Reply With Quote  
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 850
Reputation: R0bb0b is on a distinguished road 
Rep Power: 2
Solved Threads: 66
R0bb0b's Avatar
R0bb0b R0bb0b is offline Offline
Practically a Posting Shark

Re: Hiding empty <div>'s

  #7  
Jun 17th, 2008
try this
<script language="javascript">
function toggledivs(clsname, content)
{
	var divs=document.getElementsByTagName('div') 
	for (var i = 0; i < divs.length; i++)
	{
		if (divs[i].className == clsname && trim(divs[i].innerHTML) == content && divs[i].style.display != "none")
		{
			divs[i].style.display = "none";
		}
	}
}

function trim(instring) {
	instring = instring.replace(/^\s+/,"");
	return instring.replace(/\s+$/,"");
}
</script>

looks like it was the variable "class" giving the error because in ie you can't name variables the same as elements. Sorry about that.
Reply With Quote  
Join Date: Sep 2007
Posts: 25
Reputation: roy-- is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
roy-- roy-- is offline Offline
Light Poster

Re: Hiding empty <div>'s

  #8  
Jun 17th, 2008
Thanks alot. Top man.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb JavaScript / DHTML / AJAX Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Other Threads in the JavaScript / DHTML / AJAX Forum

All times are GMT -4. The time now is 2:06 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC