Click to See Complete Forum and Search --> : How to get a reference to a table through some object within it?


main
April 14th, 2003, 03:43 AM
<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>

nategrover
April 14th, 2003, 09:55 AM
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