Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > getting index to parentNode's childNodes array given an element id

Reply
Thread Tools

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

 
 
yawnmoth
Guest
Posts: n/a
 
      06-24-2006
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.chil dNodes[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!

 
Reply With Quote
 
 
 
 
Laurent Bugnion
Guest
Posts: n/a
 
      06-24-2006
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.chil dNodes[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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
element.children.length v/s element.childNodes.length okey Javascript 2 05-24-2009 04:36 PM
sorting index-15, index-9, index-110 "the human way"? Tomasz Chmielewski Perl Misc 4 03-04-2008 05:01 PM
If given 1 element in an array of textboxes... find which element number it is \A_Michigan_User\ Javascript 4 11-16-2007 12:58 PM
Remove parent element with a child element matching a given rule patrizio.trinchini@googlemail.com XML 4 08-22-2006 11:31 AM
Array#each - getting each element and the index Pat Maddox Ruby 6 01-20-2006 04:04 PM



Advertisments