toodi4 wrote on 17 nov 2006 in comp.lang.javascript
:
Quote:
I'm using a javascript that hides and unhides text based on a button
click. It works great across static fields on a form. The problem I
have is that I'm trying to hide and unhide various fields that are
populated on the page by a database. In other words, sometimes there
are 4 fields, sometimes 6.
>
I've used various scripts for the hide/unhide function. This is one
I'm using now that I've copied from another source:
>
<script language = "Javascript">
<script type='text/javascript'>
Quote:
>
var isIE=document.all?true:false;
var isDOM=document.getElementById?true:false;
var isNS4=document.layers?true:false;
This is ancient code!!!
document.getElementById() is nearly universal nowadays.
Quote:
function toggleT(_w,_h) {
if (isDOM)
{
if (_h=='s')
document.getElementById(_w).style.visibility='visi ble';
if (_h=='h') document.getElementById(_w).style.visibility='hidd en';
}
else if (isIE) {
if (_h=='s')
eval("document.all."+_w+".style.visibility='visibl e';");
Do not use eval(), it is evil, dangerous, and not necessary
Quote:
if (_h=='h')
eval("document.all."+_w+".style.visibility='hidden ';");
}
else if(isNS4)
{
if (_h=='s') eval("document.layers['"+_w+"'].visibility='show';");
if (_h=='h') eval("document.layers['"+_w+"'].visibility='hide';");
}
>}
</script>
>
>
So if in the button field I have onclick =toggleT('divt1','s');
toggleT('divt2','s'); then it will work for spans with id divt1 and
divt2.
Why not change the <spantagName collection elements in a <div>
with css style?
====================================
<script type='text/javascript'>
function hideshow(x){
var spandiv = document.getElementById('spandiv')
var spanGroup = spandiv.getElementsByTagName('SPAN')
for(i=0;i<spanGroup.length;i++)
spanGroup[i].style.visibility=(x=='hide')?'hidden':'visible'
}
</script>
<div id='spandiv'>
<span>111111</span>
===
<span>222222</span>
===
<span>333333</span>
===
<span>444444</span>
===
<span>555555</span>
</div>
<br><br>
<button onclick='hideshow("hide")'>hide</button>
<button onclick='hideshow("show")'>show</button>
============================================
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)