CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Dec 2002
    Posts
    52

    How to get a reference to a table through some object within it?

    <script language="javascript">
    function testIt(row) {
    alert(row.parentElement); //how can i get handle to the table???
    }
    </script>

    <table>
    <tr onClick=testIt(this)>
    <td>test it</td>
    </tr>
    </table>

  2. #2
    Join Date
    Feb 2003
    Location
    Oakland, CA
    Posts
    29
    It's pretty easy using DOM. If you're going to do a lot of DHTML it's best to become extremely familiar with the DOM specifications from w3c and the nuts and bolts of the MSIE and Mozilla implementations.

    Anyway, here's how you do it.

    .parentElement is IE only, so use .parentNode

    row.parentNode.parentNode will get you the table.

    do safely traverse the DOM tree for a specific parent without knowing exactly how far up the tree you need to go, you should do something like this:

    var p = theNode.parentNode;
    while(p.tagName != ""BODY"){
    if(p.tagName == "TABLE") break;
    else{
    p = p.parentNode;
    }
    }

    Hope this helps.

    Nate Grover
    http://www.nategrover.com
    Nate Grover
    http://www.nategrover.com

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured