child node position

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • theS70RM
    New Member
    • Jul 2007
    • 107

    child node position

    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:

    Code:
    <div id="theparent">
        <div id=adiv>something</div>
        <div id=anotherdiv>something</div>
        <div id=divtrois>something</div>
        <div id=mydiv>something</div>
    </div>

    and then are given

    Code:
    alert(getDivIndex(document.getElementById('anotherdiv')));  //returns 2
    
    alert(getDivIndex(document.getElementById('mydiv')));  //returns 4
    I know it's fairly easy to code, im just wondering what the quickest way possible is.



    Cheers

    Andy
  • theS70RM
    New Member
    • Jul 2007
    • 107

    #2
    wow it totally but extra spaces in my code, why'd it do that?? :-S

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      Using getElementsByTa gName("div") and looping over checking for the ID would probably be the easiest.
      Originally posted by theS70RM
      wow it totally but extra spaces in my code, why'd it do that?? :-S
      A code tag bug.

      Comment

      • Rsmastermind
        New Member
        • Sep 2008
        • 93

        #4
        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.
        getElementsByTa gName("*");
        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.

        Comment

        Working...