Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically identify layers? 1

Status
Not open for further replies.

ClulessChris

IS-IT--Management
Jan 27, 2003
890
GB
I'm brand new to JavaScript so please excuse my ignorance.

Using an external script file I have the following function:
Code:
Function hideAll(){
	hideLayer('layer1');
	hideLayer('layer2');
	hideLayer('layer3');
	hideLayer('layer4');
	}
Is there a method (such as a loop) where I can dynamically identify and hide all layer rather than implicitly naming each?
I would like to use this external Script file for all pages, but not all pages will have all layers.

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
You can use documents.getElementsByTagName('div') (or whatever container you want to work with).

Lee
 
Chris, you can use the getElementsByTagName method which basically takes all tags of a certain type and sticks them into an array however lets say your "layers" are made from divs then this will hide all your divs when implemented into a function.

Another way would be to use the getElementsById like do

Code:
function hideAll()

for (i=0;i<=4;i++){
hideLayer(document.getElementsByTagName('Layer'+i).id)
}
}

So if you know how many "layers" you have then you could use a loop like the one above.

Cheers

Nick
 
Both thanks for your responces.
However I'm not quite following.
I can see how this will loop (1 to 4) but can I dynamicly determine howmany layers are in a given sheet?
i.e. page 1 has 4 layers but page 2 has 2.
If I loop 4 times the code will crash when looking for layer3.

Sorry for being dim, I'm well out of my waters here.

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
I guess it should mean this.
[tt]
function hideAll() { //f in lower case
var clayer=document.getElementsByTagName("layer");
for (var i=0;i<clayer.length;i++) {
hideLayer(clayer.name);
//.name or .id just can't be sure
//clayer[0].name or clayer[0].id should reproduce "layer1", for instance
}
}[/tt]
 
If your layers are DIV elements, you can use the following for "n" DIVs:

Code:
function hideAll() {
	var divNum = 1;
	while (divEl = document.getElementById('layer' + divNum)) {
		hideLayer(divEl.id);
		divNum++;
	}
}

This assumes the DIVs have an ID of "layer1", "layer2", etc.

Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
fantastic, many thanks too all for your input.

Never knock on Death's door: ring the bell and run away! Death really hates that!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top