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!

<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>

Thanks, trying to use the function:

toggledivs('policies', '');

keep getting errors though, am not sure i understand how to use the function

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.)

tried that, nothing happens in firefox, and internet explorer gives an object expected error ;/

funny, works in ff for me just fine, but I do get an error in IE. Looking for a workaround.

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.

This article has been dead for over six months. Start a new discussion instead.