Forum Moderators: open

Message Too Old, No Replies

Sorting objects

getting the id back from an object

         

ragedbull

2:29 am on Mar 31, 2005 (gmt 0)

10+ Year Member



Here is my code:

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

Bernard Marx

7:10 am on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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.

SpaceFrog

7:20 am on Mar 31, 2005 (gmt 0)

10+ Year Member



My try :

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

Bernard Marx

10:37 am on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Er...what's the point?

SpaceFrog

12:06 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



in my try?

well it's just that it took me 10 minutes more than you and I had not seen your reply ... ;-)

Bernard Marx

12:41 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes. Gotta watch out for that one indeed.
I was really referring to the population of the array of ids before taking any action.
All good fun, I suppose.

ragedbull

7:50 pm on Mar 31, 2005 (gmt 0)

10+ Year Member



Thank You! I don't use javascript very often anymore, and I kept trying .getId() or something else- I'm glad to see it is so simple, and its works in both ie and ff! Thanks! I will experiments with putting the entire menu i want to be operated upon in one giant div tag and then call the command so it only goes inside there, the looping seems to take too long for my tastes in ie, and I assume if i only go through a small list it will be quicker!

Thank You again!

Bernard Marx

10:02 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



An alternative route could be to give all those elements a common className then switch, or add to, the className of the overall parent. showing¦hiding can be done via a contextual CSS selector. This way, no looping would be required.