getting index to parentNode's childNodes array given an element id

Discussion in 'Javascript' started by yawnmoth, Jun 24, 2006.

  1. yawnmoth

    yawnmoth Guest

    Given an element ID, is there a way to figure out what index one would
    need to use in the parentNode's childNodes array to get at that
    element?

    For example...

    <body>
    <div id="parent">
    <div id="a">a</div>
    <div id="b">b</div>
    <div id="c">c</div>
    </div>

    <script type="text/javascript">

    alert(document.getElementById('a').parentNode.childNodes[0].innerHTML);
    </script>
    </body>

    The index that IE uses (0) isn't the same as the index that Firefox
    uses (1). How could I find this out given an element ID?

    As for why I'm asking... I'm writting a script some of whose elements
    are not going to be in any predictable order. I'd like to give each of
    these elements their own unique ID and then figure out each of their
    indexes to their collective parents (they all have the same parent)
    childNodes array. This will allow the HTML to be aware of the order of
    these elements even though the server side script, itself, isn't.

    The above script doesn't really seem relevant, at first, to this
    problem, but if I can solve the problem in the above script, I think I
    should be able to solve the problem in the actual script I'm working
    on.

    Anyway, any ideas would be appreciated - thanks!
     
    yawnmoth, Jun 24, 2006
    #1
    1. Advertising

  2. Re: getting index to parentNode's childNodes array given an elementid

    Hi,

    yawnmoth wrote:
    > Given an element ID, is there a way to figure out what index one would
    > need to use in the parentNode's childNodes array to get at that
    > element?
    >
    > For example...
    >
    > <body>
    > <div id="parent">
    > <div id="a">a</div>
    > <div id="b">b</div>
    > <div id="c">c</div>
    > </div>
    >
    > <script type="text/javascript">
    >
    > alert(document.getElementById('a').parentNode.childNodes[0].innerHTML);
    > </script>
    > </body>
    >
    > The index that IE uses (0) isn't the same as the index that Firefox
    > uses (1). How could I find this out given an element ID?


    Actually, IE and Firefox use the same indexing system, but the
    difference is that FF counts carriage returns in the HTML document as a
    valid text node, when IE doesn't. IMHO, and referring to XML, I think
    that IE is right and FF wrong.

    About your question, short of writing a function looping through all
    childnodes and comparing to the current element, I don't know any
    property allowing to find this.

    function getIndex( el )
    {
    for ( var index = 0;
    index < el.parentNode.childNodes.length; index++ )
    {
    if ( el == el.parentNode.childNodes[ index ] )
    {
    return index;
    }
    }
    return -1;
    }

    and

    var el = document.getElementById('a');
    var indexOfEl = getIndex( el );
    alert( el.parentNode.childNodes[ indexOfEl ].innerHTML );

    Untested, but it should work.

    HTH,
    Laurent

    > As for why I'm asking... I'm writting a script some of whose elements
    > are not going to be in any predictable order. I'd like to give each of
    > these elements their own unique ID and then figure out each of their
    > indexes to their collective parents (they all have the same parent)
    > childNodes array. This will allow the HTML to be aware of the order of
    > these elements even though the server side script, itself, isn't.


    I can think of a few ways to create the information on the server, and
    then pass it to the client-side script. But then, maybe I didn't
    understand wat you try to achieve.

    [snip]

    HTH,
    Laurent
    --
    Laurent Bugnion, GalaSoft
    Software engineering: http://www.galasoft-LB.ch
    Private/Malaysia: http://mypage.bluewin.ch/lbugnion
    Support children in Calcutta: http://www.calcutta-espoir.ch
     
    Laurent Bugnion, Jun 24, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    3,449
  2. Pat Maddox
    Replies:
    6
    Views:
    158
    Marcin Mielżyński
    Jan 20, 2006
  3. Tomasz Chmielewski

    sorting index-15, index-9, index-110 "the human way"?

    Tomasz Chmielewski, Mar 4, 2008, in forum: Perl Misc
    Replies:
    4
    Views:
    307
    Tomasz Chmielewski
    Mar 4, 2008
  4. \A_Michigan_User\
    Replies:
    4
    Views:
    155
    Thomas 'PointedEars' Lahn
    Nov 16, 2007
  5. okey
    Replies:
    2
    Views:
    168
    David Mark
    May 24, 2009
Loading...

Share This Page