•
•
•
•
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 401,486 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 3,253 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: 731 | Replies: 7
![]() |
•
•
Join Date: Sep 2007
Posts: 25
Reputation:
Rep Power: 1
Solved Threads: 0
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!
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!
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 599
Reputation:
Rep Power: 2
Solved Threads: 53
<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 7:00 am.
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 599
Reputation:
Rep Power: 2
Solved Threads: 53
Your going to use a function call like this
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.)
<a href="javascript:toggledivs('policy_text', '');">toggle divs</a>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 9:34 am.
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 599
Reputation:
Rep Power: 2
Solved Threads: 53
•
•
Join Date: Jun 2008
Location: Phoenix, AZ
Posts: 599
Reputation:
Rep Power: 2
Solved Threads: 53
try this
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.
<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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: unable to send a HttpRequest
- Next Thread: Password protect with cookie


Linear Mode