473,395 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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!

Jun 24 '06 #1
1 8230
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
Jun 24 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Weston C | last post by:
Here's something I'm working on: for a click on a given element, I want to be able capture the x/y coordinates of the mouse -- that is, the mouse coordinates relative to the top left corner of said...
4
by: sugoi.sama | last post by:
hi, i'm having a hellish adventure with PHP5 i hope someone just can help me out on this... i'm desperate All i want to do, is to get the index of the returned elements, so i can acess them...
11
by: motion musso aka: sathia | last post by:
this is it, how can i get the current index? couldn't figure it out. thank you for(i=0;myarray.length<i;i++){ do_something(); //i need the current myarray } bye bye
3
by: Håkan Johansson | last post by:
Being new to C# I wonder if the index of an array can be some other ordinal type than int? Coming from Delphi, I can determine the type of the index as well as the type of the elements, but haven't...
3
by: Jerry West | last post by:
I'd like to get the upper bound index of an integer array. I've tried the following: Dim i as Integer Dim arrayIng() as Integer i = arrayIng.GetUpperBound This doesn't work. It seems...
4
by: SM | last post by:
Hello, Knowing the index position of an array, how can i get the next $key available, if it exists. I've found a solution but I was wondering if there was a shorcut in PHP. i.e: $cats =...
9
by: =?Utf-8?B?R2Vla0JveQ==?= | last post by:
I did not see another group for this so I presume this is the correct one to ask such a question. Anyway, I need to get data entered into a textbox and put it all into an array. I have looked...
4
by: raylopez99 | last post by:
I would like to know if there's a quick "Linq" way to find the index of an array having a particular value. I can do this the long way by sequential iteration, but would like to know if there's a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.