Is there getAllElementIds() function?

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

    Is there getAllElementIds() function?

    The Subject says its all.

    document.getEle mentById('id');

    only return an object with id name.

    I am looking for a function
    return array of objects.

    document.getAll ElementIds();

    BTW, I am also looking for

    document.getAll ElementNames();
    document.getEle mentByName("nam e");

    function?
    where "name" is from HTML

    <tag name="document_ name" ....>
    similar to
    <tag id="id_name" ....>

    document.anchor s return array of <a>anchor</a>
    document.forms return array of <form>
    document.images return array of <img>
    document.links return array of <a>
    But why there is no
    document.ids return array of ids?
  • Matt Kruse

    #2
    Re: Is there getAllElementId s() function?

    RC wrote:[color=blue]
    > I am looking for a function
    > return array of objects.
    > document.getAll ElementIds();[/color]

    Why?

    When you're using ID's, which must be unique, it's assumed that you know
    what is on the page and what its ID is. Otherwise, why would it have an ID?

    --
    Matt Kruse




    Comment

    • RobG

      #3
      Re: Is there getAllElementId s() function?

      RC wrote:[color=blue]
      > The Subject says its all.
      >
      > document.getEle mentById('id');
      >
      > only return an object with id name.
      >
      > I am looking for a function
      > return array of objects.[/color]

      There isn't one. You could use:

      var allEls = document.getEle mentsByTagName( '*');

      then sift through the returned collection to find all elements using
      (untested):

      var el, idEls=[];
      var i = allEls.length;
      while (i--) {
      el = allEls[i];
      if ( el.id && el.id != '') idEls.push(el);
      }

      But I expect that will be very slow and ridiculously processor
      intensive. Explain more about what you are trying to do and maybe
      someone will have a better way to achieve it.

      [color=blue]
      > document.getAll ElementIds();
      >
      > BTW, I am also looking for
      >
      > document.getAll ElementNames();[/color]

      You could use something like that above for IDs, however elements less
      than half the elements in HTML 4 can have a name attribute.

      [color=blue]
      > document.getEle mentByName("nam e");[/color]

      There is getElementsByNa me() already, it returns a collection. If you
      know only one element has a particular name, or you want the first one
      with that name, then:

      var namedEl = document.getEle mentsByName('so meName')[0];


      should do the trick.

      [color=blue]
      > function?
      > where "name" is from HTML
      >
      > <tag name="document_ name" ....>
      > similar to
      > <tag id="id_name" ....>
      >
      > document.anchor s return array of <a>anchor</a>
      > document.forms return array of <form>
      > document.images return array of <img>
      > document.links return array of <a>
      > But why there is no
      > document.ids return array of ids?[/color]

      Explain why you want that. All of the above are collections of one
      particular type of element, document.ids (if it existed) could be a
      mixed collection of nearly any type of element.


      --
      Rob

      Comment

      • RC

        #4
        Re: Is there getAllElementId s() function?

        RobG wrote:
        [color=blue]
        > Explain more about what you are trying to do and maybe
        > someone will have a better way to achieve it.[/color]

        First, thank Q very much for the long reply.

        If my HTML file have 20 or more id names like

        <div id="id1">
        ....
        <div id="idN">

        I really don't want to do

        var arrayOfId = new Array(20);

        arrayOfId[0] = document.getEle mentById('id1') ;
        ....
        arrayOfId[19] = document.getEle mentById('idN') ;

        will be nice if I can

        var arrayOfId[] = document.getAll ElementIds();

        In my case, I am only interest <div id="...">

        If I name all div in the same name like

        <div name="mydiv" id="id1">
        ....
        <div name="mydiv" id="idN">

        Then can I

        var mydivname = document.getEle mentsByName("my div");
        var arrayIds = new Array(mydivname .id.lenght);
        for (var i = 0; i < arrayIds.length ; i++)
        arrayIds[i] = document.getEle mentById(mydivn ame.id[i].value);

        Comment

        • Rob

          #5
          Re: Is there getAllElementId s() function?

          In your code:
          var mydivname = document.getEle mentsByName("my div");
          var arrayIds = new Array(mydivname .id.lenght);
          This does not create an array of .length
          for (var i = 0; i < arrayIds.length ; i++) //So this .lenght is always
          one
          arrayIds[i] = document.getEle mentById(mydivn ame.id[i].value);// this
          is wrong in several ways.

          I rewrote and tested your code below.
          <div name="mydiv" id="id1">ID1</div>
          <div name="mydiv" id="id2">ID2</div>
          <script>
          var mydivname = document.getEle mentsByName("my div");
          alert("Found "+mydivname.len gth+" elements with name mydiv")
          var arrayIds = new Array();
          for (var i = 0; i < mydivname.lengt h; i++) {
          arrayIds.push(d ocument.getElem entById(mydivna me[i].id));
          alert("arrayIds["+i+"] is "+arrayIds[i].id);
          }
          </script>
          So I'm still not clear on what you are trying to accomplish. Now you
          have an array which contains all your DIV ids. I'm not sure I see an
          applicaton for such an array (but I'm new at this so I may be missing
          something:).

          Rob:-]

          Comment

          • RC

            #6
            Re: Is there getAllElementId s() function?

            Rob wrote:
            [color=blue]
            > So I'm still not clear on what you are trying to accomplish. Now you
            > have an array which contains all your DIV ids. I'm not sure I see an
            > applicaton for such an array (but I'm new at this so I may be missing
            > something:).[/color]

            Hmhmhmhmhm..... ...hahaha...!!!

            I am try to make my own pull down menu in DHTML = JavaScript + CSS
            I know there are many JavaScript Menu out there to download.
            But they all require you have greate amount of JavaScript programming
            skills. I hate to read somebody's poor documents.

            So I designed write by my own. Just HTML with
            minimum JavaScript for onMouseOver, onMouseOut and onClick
            plus a CSS file.

            <a href=# onMouseOver="pu lldown('mydiv0' )">Menu 0</a>

            <a href=# onMouseOver="pu lldown('mydiv1' )">Menu 1</a>
            .....

            <a href=# onMouseOver="pu lldown('mydivN' )">Menu N</a>

            You really can't do onMouseOut="col lapse('mydivid' )"
            Because let's say you move mouse pointer to Menu 1,
            it do pulldown for mydiv1, greate! but once you
            move the pointer to the sub-menu in Menu 1, it then do
            collapse for Menu 1. so I NEVER able to select sub-menu from
            Menu 1.

            Therefore, when I move mouse pointer to Menu 1, I want to make sure
            Menu 0, Menu 2, Menu N all are collapse if the are/were pull down.
            Similar, when I move mouse pointer to Menu 0, Menu 1, Menu 2, ..
            will collapse.

            Now you understand why I want get an array of <div name="mydiv" id="xx">
            ?

            Hahahaha....... .!!!

            Again, thank Q very much to the test for me. I'll use your part of codes
            to finish my last part of programs

            Comment

            • Rob

              #7
              Re: Is there getAllElementId s() function?

              So you might try this code in your anchor tags:

              <div name="mydiv" id="id1"><a href="#"
              onMouseOver="pu lldown(this.par entNode.id);">I D1</div>
              <div name="mydiv" id="id2"><a href="#"
              onMouseOver="pu lldown(this.par entNode.id);">I D2</div>
              <script>
              function pulldown(divId) {
              alert("onMouseO ver("+divId+")" );
              }
              var mydivname = document.getEle mentsByName("my div");
              //alert("Found "+mydivname.len gth+" elements with name mydiv")
              var arrayIds = new Array();
              for (var i = 0; i < mydivname.lengt h; i++) {
              arrayIds.push(d ocument.getElem entById(mydivna me[i].id));
              // alert("arrayIds["+i+"] is "+arrayIds[i].id);
              }
              </script>

              This way the only thing you have to change on each menu item is the DIV
              id. (Well, I guess you'll be filling in actual href URLs too:)

              I tested it and it works.

              Rob:-]

              Comment

              • RobG

                #8
                Re: Is there getAllElementId s() function?

                RC wrote:[color=blue]
                > RobG wrote:
                >[color=green]
                >> Explain more about what you are trying to do and maybe someone will
                >> have a better way to achieve it.[/color]
                >
                > First, thank Q very much for the long reply.
                >
                > If my HTML file have 20 or more id names like
                >
                > <div id="id1">
                > ...
                > <div id="idN">
                >
                > I really don't want to do
                >
                > var arrayOfId = new Array(20);
                >
                > arrayOfId[0] = document.getEle mentById('id1') ;
                > ...
                > arrayOfId[19] = document.getEle mentById('idN') ;[/color]

                You could use a loop, and if there are only 20 divs it would be
                reasonably fast:

                var arrayOfId = [];
                for (var i=0; i<20; ++i) {
                arrayOfId[i] = document.getEle mentById('id' + i)
                }

                or if the divs are numbered consecutively and contiguously:

                var arrayOfId = [];
                var i = 0;
                var div;
                while ( (div=document.g etElementById(' id' + i++)) ){
                arrayOfId[i] = div;
                }

                with appropriate feature testing.

                [color=blue]
                > will be nice if I can
                >
                > var arrayOfId[] = document.getAll ElementIds();
                >
                > In my case, I am only interest <div id="...">
                >
                > If I name all div in the same name like
                >
                > <div name="mydiv" id="id1">
                > ...
                > <div name="mydiv" id="idN">[/color]

                'name' is not a valid attribute for a div element in HTML 4.

                <URL:http://www.w3.org/TR/html4/struct/global.html#ede f-DIV>

                Using names and getElementsByNa me with divs does work in some browsers,
                but you can't expect it to work everywhere.

                [color=blue]
                > Then can I
                >
                > var mydivname = document.getEle mentsByName("my div");
                > var arrayIds = new Array(mydivname .id.lenght);
                > for (var i = 0; i < arrayIds.length ; i++)
                > arrayIds[i] = document.getEle mentById(mydivn ame.id[i].value);[/color]

                getElementsByNa me() returns a collection, which has some of the
                properties of an array. What array properties do you want to use that a
                collection doesn't have? There are many simple drop-down menus that use
                little or no script, most are based on UL/LI elements.

                Google is your friend, there are a number of them on
                <URL:http://www.alistapart. com/>.


                --
                Rob

                Comment

                • Richard Cornford

                  #9
                  Re: Is there getAllElementId s() function?

                  RC wrote:
                  <snip>[color=blue]
                  > Now you understand why I want get an array of <div
                  > name="mydiv" id="xx"> ?[/color]
                  <snip>

                  The HTML DTDs do not define a NAME attribute for DIV elements.
                  Consequently not all browsers will return DIV elements with NAME
                  attributes through the - document.getEle mentsByName - method. As an
                  attempt to produce cross-browser results your strategy is already dead
                  in the water.

                  Richard.


                  Comment

                  Working...