SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to get a reference to next <td>?

    i'm wondering how i can get a reference to the next td in a row? Do i need to assign all td's an id or something? i could do that failry easily because the table gets generated dynamically via a php function.. i just hink this would be nice that way when i go to delete something (with those infamous checkboxes) i would like the javascript prompt to display what it is that will be deleted..

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have a reference to the TABLE element (e.g. via an ID), you can use its childNodes() method to access its immediate children (and then apply that recursively).

    You can also use the firstChild property to get a reference to the first child, and then the nextSibling property of the child to traverse the list of child nodes.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict
    Join Date
    Nov 2003
    Location
    Malmoe, Sweden
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use DOM. If you have a reference to a <td> then you could use nextSibling. Be careful though as there are browser differences. Some count white-spaces and has not implemented normalization so you will get different results with these to examples:
    Code:
    <tr>
    	<td>Foo</td>
    	<td>Bar</td>
    </tr>
    Code:
    <tr><td>Foo</td><td>Bar</td></tr>
    Edit
    Hmm. Didnīt see your post Tommy. No intention repeating exactly what you said :-P

  4. #4
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <tr><td>Foo</td><td>Bar</td></tr>

    using the above example, lets say i have a reference to the row.. which i know i have.. how do i get to foo or to bar..lets call the reference inputReference..
    inputReference.firstChild.nextSibling.nodeValue didnt work...

  5. #5
    SitePoint Addict
    Join Date
    Nov 2003
    Location
    Malmoe, Sweden
    Posts
    265
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    inputReference.firstChild.nextSibling.firstChild.nodeValue

    should work.

    inputReference is tr.

    inputReference.firstChild is first td.

    inputReference.firstChild.nextSibling is second td.

    inputReference.firstChild.nextSibling.firstChild is the text node containing 'bar'.

    inputReference.firstChild.nextSibling.firstChild.nodeValue is the value of that text node. The string 'bar'.

  6. #6
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    duhh.. sorry, i'm very new to js, works perfect though, thank you

  7. #7
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by andrin
    Hmm. Didnīt see your post Tommy. No intention repeating exactly what you said :-P
    That's not a problem. It would have been much worse if you had contradicted me.
    Birnam wood is come to Dunsinane

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ....or

    inputReference.getElementsByTagName("td")[1].firstChild.nodeValue

    where

    inputReference.getElementsByTagName("td")

    is an array of all the <td>'s in the row, and [1] is the second element in the array(arrays are 0 based).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •