By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,504 Members | 1,092 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,504 IT Pros & Developers. It's quick & easy.

child node position

100+
P: 107
Whats the quickest way to get the position of a node in its parent, i dont mean the physical position, i mean the index order....

So if you have:

Expand|Select|Wrap|Line Numbers
  1. <div id="theparent">
  2.     <div id=adiv>something</div>
  3.     <div id=anotherdiv>something</div>
  4.     <div id=divtrois>something</div>
  5.     <div id=mydiv>something</div>
  6. </div>

and then are given

Expand|Select|Wrap|Line Numbers
  1. alert(getDivIndex(document.getElementById('anotherdiv')));  //returns 2
  2.  
  3. alert(getDivIndex(document.getElementById('mydiv')));  //returns 4
  4.  
  5.  
  6.  
  7.  
I know it's fairly easy to code, im just wondering what the quickest way possible is.



Cheers

Andy
Sep 18 '08 #1
Share this Question
Share on Google+
3 Replies


100+
P: 107
wow it totally but extra spaces in my code, why'd it do that?? :-S
Sep 18 '08 #2

acoder
Expert Mod 15k+
P: 16,027
Using getElementsByTagName("div") and looping over checking for the ID would probably be the easiest.
wow it totally but extra spaces in my code, why'd it do that?? :-S
A code tag bug.
Sep 18 '08 #3

P: 93
Hi
If you want to know the position and if it is tag specific
like <div>,<table>.......
then the solution provided by acoder is ok.

If you dont know the tag name you can give wild card in this way.
getElementsByTagName("*");
this will return you the array of all nodes in your dcument irrespective of the
DOM structure.

Yes this will take your space .But the thing is that in current scenario u know the document.But suppose if you want to do it from external centralised Js file.
then it will be very easy.U can write it once and can use for any number of documents.
Sep 19 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.