By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,300 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

Wildcard for getElementById

P: n/a
Is there any wildcard, like *, for addressing all the element ID's on the
page at once, like if you want to hide all layers at once. For example:

document.getElementById('*').style.visibility = 'hidden';

I know the above doesn't work but you get what I'm driving at right? Is
there anything like that for that method or in javascript in general?
Thanks.
Jul 20 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
<script language="javascript">
/* browser compatibility NOT tested!
For this script, you have your 'parent' activator, and you have your target div
layer.
when you call the function in the html page
ie: onClick="changeDiv('child_layer_id')"
you pass the child layer's id which you
want to only show while all the other
divs are hidden; of course, just comment out the display:block code and all
the divs on the page (with the special attribute code) remain hidden
*/

function changeDiv(divid){

// create object of all div tags in the document
var tag = document.getElementsByTagName('DIV')

// create the child layer object using the argument passed to the function
var elid = document.getElementById(divid);

//the show/hide layer has an attribute nav="yes" in it
// to distiguish it from other DIV elements in the page
// by determining if that DIV element has the attribute
// i can make sure i'm affecting the show/hide layer DIV

if(elid.getAttribute("nav")){
for(x=0; x<tag.length; x++){
//hide all div layers with the 'nav' attribute
if(tag[x].getAttribute("nav")){
tag[x].style.display="none";
}
// show only the layer with the id passed as the argument
// by the main function
elid.style.display="block";
}
}
}
</script>
Hope this helps!

~Jim
Jul 20 '05 #2

P: n/a
"TheKeith" <no@spam.com> writes:
Is there any wildcard, like *, for addressing all the element ID's on the
page at once, like if you want to hide all layers at once. For example:

document.getElementById('*').style.visibility = 'hidden';
No. You only get one element with getElementById, so using a wildcard
doesn't make sense. Even if it returned more than one element, you can't
use .style.visiblity on the collection and hope it affects the contents.
I know the above doesn't work but you get what I'm driving at right? Is
there anything like that for that method or in javascript in general?


What you can use instead is:

var elems = document.getElementsByTagName("*"); // yes, wildcards do exist
for (var i=0;i<elems.length;i++) {
if ( .... elems[i] .... ) { // you probably don't want to hide *all* elements
elems[i].style.visibility="hidden";
}
}
/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Demo: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #3

P: n/a

"JimMenees" <ji*******@aol.comNoSpam> wrote in message
news:20***************************@mb-m23.aol.com...
<script language="javascript">
/* browser compatibility NOT tested!
For this script, you have your 'parent' activator, and you have your target div layer.
when you call the function in the html page
ie: onClick="changeDiv('child_layer_id')"
you pass the child layer's id which you
want to only show while all the other
divs are hidden; of course, just comment out the display:block code and all the divs on the page (with the special attribute code) remain hidden
*/

function changeDiv(divid){

// create object of all div tags in the document
var tag = document.getElementsByTagName('DIV')

// create the child layer object using the argument passed to the function
var elid = document.getElementById(divid);

//the show/hide layer has an attribute nav="yes" in it
// to distiguish it from other DIV elements in the page
// by determining if that DIV element has the attribute
// i can make sure i'm affecting the show/hide layer DIV

if(elid.getAttribute("nav")){
for(x=0; x<tag.length; x++){
//hide all div layers with the 'nav' attribute
if(tag[x].getAttribute("nav")){
tag[x].style.display="none";
}
// show only the layer with the id passed as the argument
// by the main function
elid.style.display="block";
}
}
}
</script>
Hope this helps!

Thanks a lot. I was really just hoping there was a wild card in general, but
thanks anyway. The visbility was just an example.
Jul 20 '05 #4

P: n/a

"Lasse Reichstein Nielsen" <lr*@hotpop.com> wrote in message
news:oe**********@hotpop.com...
"TheKeith" <no@spam.com> writes:
Is there any wildcard, like *, for addressing all the element ID's on the page at once, like if you want to hide all layers at once. For example:

document.getElementById('*').style.visibility = 'hidden';
No. You only get one element with getElementById, so using a wildcard
doesn't make sense. Even if it returned more than one element, you can't
use .style.visiblity on the collection and hope it affects the contents.
I know the above doesn't work but you get what I'm driving at right? Is
there anything like that for that method or in javascript in general?


What you can use instead is:

var elems = document.getElementsByTagName("*"); // yes, wildcards do

exist for (var i=0;i<elems.length;i++) {
if ( .... elems[i] .... ) { // you probably don't want to hide *all* elements elems[i].style.visibility="hidden";
}
}

thanks a lot. Why is it that I don't see the getElementByTagName method in
the javascript reference in the newest version of dreamweaver (mx 2004)? Is
it really new or something?
Jul 20 '05 #5

P: n/a
"TheKeith" <no@spam.com> writes:
Why is it that I don't see the getElementByTagName method in the
javascript reference in the newest version of dreamweaver (mx 2004)?
I'll blame whoever made dreamweaver. But I do that already, having seen
the Javascript it embeds in the pages it makes.
Is it really new or something?


The getElementsByTagName function was part of the W3C DOM 1, which was
made a recommendation in October 1998. Ofcourse, browsersupport lacked
behind, and Windows IE only supported it from version 5.0 (March
1999), Opera from version 5 (December 2000), and Mozilla/Netscape 6+
from the beginning (no exact date, Netscape 6 was based on a pre-1.0
version of Mozilla, and was released in November 2000, but people had
been using builds of Mozilla before that).

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.