Forum Moderators: open
function Collapse() {
divs=document.getElementsByTagName("DIV");
alert(divs[0]. toString());
for (i=0;i<divs.length;i++) {
//var xRegEx = new RegExp("x^","i");
//if(xRegEx.exec(divs[i] == null))
myString = "It is: " + divs[i].toString();
alert(myString);
//if(myString.match(\x{1}[a-z]+\b))
if(myString.charAt(0) = 'x')
{
divs[i].style.display="none";
}
}
}
What I am trying to do is hide all the DIV tags where the ID of the tag starts with an x.
I am doing a cascading-tree style menu, and when i use this collapse function, it hides other objects on the website that are not menu objects! Therefore, I only want to hide <div> elements that start with an x, not if they start with anything else or do not have an id tag period. Any help would be appreciated! I hope I explained this well
function Collapse()
{
var divs, i, xReg; /* use local vars */
xReg = /^x/i;
divs = document.getElementsByTagName("DIV");
for (i=0;i<divs.length;i++)
if( xReg.test(divs[i].id) )
divs[i].style.display = 'none';
}
If all the targetted divs are inside a common parent, it would be more efficient to call the getElementsByTagName method from that element, rather than the whole document.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Nouvelle page 1</title>
<script type='text/javascript' >
function treehide(){
var divs=document.getElementsByTagName('div');
var divid=new Array()
for (i=0;i<divs.length;i++){
divid[divid.length]=divs[i].id
}
for (i=0;i<divid.length;i++){
divs[i].style.display=(divid[i].split('')[0]=='d')?'none':'block';
}
}
</script>
</head>
<body onload="treehide()">
<div id="de" >dd</div>
<div id="des" >dd</div>
<div id="ded" >dd</div>
<div id="fdeg" >dd</div>
<div id="dez" >dd</div>
<div id="den" >dd</div>
<div id="dek" >dd</div>
<div id="fdel" >dd</div>
<div id="dem" >dd</div>
<div id="de" >dd</div>
<div id="fdes" >dd</div>
<div id="ded" >dd</div>
<div id="deg" >dd</div>
<div id="dez" >dd</div>
<div id="fden" >dd</div>
<div id="dek" >dd</div>
<div id="del" >dd</div>
<div id="dem" >dd</div>
</body>
</html>
Thank You again!