Connecting Tech Pros Worldwide Forums | Help | Site Map

Wildcard for getElementById

TheKeith
Guest
 
Posts: n/a
#1: Jul 20 '05
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.



JimMenees
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Wildcard for getElementById


<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
Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Wildcard for getElementById


"TheKeith" <no@spam.com> writes:
[color=blue]
> 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';[/color]

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.
[color=blue]
> 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?[/color]

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 - lrn@hotpop.com
DHTML Demo: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
TheKeith
Guest
 
Posts: n/a
#4: Jul 20 '05

re: Wildcard for getElementById



"JimMenees" <jimmenees@aol.comNoSpam> wrote in message
news:20031030001308.04022.00000362@mb-m23.aol.com...[color=blue]
> <script language="javascript">
> /* browser compatibility NOT tested!
> For this script, you have your 'parent' activator, and you have your[/color]
target div[color=blue]
> 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[/color]
all[color=blue]
> 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![/color]


Thanks a lot. I was really just hoping there was a wild card in general, but
thanks anyway. The visbility was just an example.


TheKeith
Guest
 
Posts: n/a
#5: Jul 20 '05

re: Wildcard for getElementById



"Lasse Reichstein Nielsen" <lrn@hotpop.com> wrote in message
news:oevzypd9.fsf@hotpop.com...[color=blue]
> "TheKeith" <no@spam.com> writes:
>[color=green]
> > Is there any wildcard, like *, for addressing all the element ID's on[/color][/color]
the[color=blue][color=green]
> > page at once, like if you want to hide all layers at once. For example:
> >
> > document.getElementById('*').style.visibility = 'hidden';[/color]
>
> 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.
>[color=green]
> > 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?[/color]
>
> What you can use instead is:
>
> var elems = document.getElementsByTagName("*"); // yes, wildcards do[/color]
exist[color=blue]
> for (var i=0;i<elems.length;i++) {
> if ( .... elems[i] .... ) { // you probably don't want to hide *all*[/color]
elements[color=blue]
> elems[i].style.visibility="hidden";
> }
> }[/color]


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?


Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#6: Jul 20 '05

re: Wildcard for getElementById


"TheKeith" <no@spam.com> writes:
[color=blue]
> Why is it that I don't see the getElementByTagName method in the
> javascript reference in the newest version of dreamweaver (mx 2004)?[/color]

I'll blame whoever made dreamweaver. But I do that already, having seen
the Javascript it embeds in the pages it makes.
[color=blue]
> Is it really new or something?[/color]

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 - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread


Similar JavaScript / Ajax / DHTML bytes