Wildcard for getElementById

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TheKeith

    Wildcard for getElementById

    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.getEle mentById('*').s tyle.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

    #2
    Re: Wildcard for getElementById

    <script language="javas cript">
    /* 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="change Div('child_laye r_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.getEle mentsByTagName( 'DIV')

    // create the child layer object using the argument passed to the function
    var elid = document.getEle mentById(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.getAttr ibute("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.disp lay="block";
    }
    }
    }
    </script>
    Hope this helps!

    ~Jim

    Comment

    • Lasse Reichstein Nielsen

      #3
      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.getEle mentById('*').s tyle.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.visiblit y 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.getEle mentsByTagName( "*"); // yes, wildcards do exist
      for (var i=0;i<elems.len gth;i++) {
      if ( .... elems[i] .... ) { // you probably don't want to hide *all* elements
      elems[i].style.visibili ty="hidden";
      }
      }


      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Demo: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • TheKeith

        #4
        Re: Wildcard for getElementById


        "JimMenees" <jimmenees@aol. comNoSpam> wrote in message
        news:2003103000 1308.04022.0000 0362@mb-m23.aol.com...[color=blue]
        > <script language="javas cript">
        > /* 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="change Div('child_laye r_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.getEle mentsByTagName( 'DIV')
        >
        > // create the child layer object using the argument passed to the function
        > var elid = document.getEle mentById(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.getAttr ibute("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.disp lay="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.


        Comment

        • TheKeith

          #5
          Re: Wildcard for getElementById


          "Lasse Reichstein Nielsen" <lrn@hotpop.com > wrote in message
          news:oevzypd9.f sf@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.getEle mentById('*').s tyle.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.visiblit y 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.getEle mentsByTagName( "*"); // yes, wildcards do[/color]
          exist[color=blue]
          > for (var i=0;i<elems.len gth;i++) {
          > if ( .... elems[i] .... ) { // you probably don't want to hide *all*[/color]
          elements[color=blue]
          > elems[i].style.visibili ty="hidden";
          > }
          > }[/color]


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


          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Wildcard for getElementById

            "TheKeith" <no@spam.com> writes:
            [color=blue]
            > Why is it that I don't see the getElementByTag Name 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 getElementsByTa gName 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/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            Working...