Re: How can I find the name of the parent table from a table cell?
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: How can I find the name of the parent table from a table cell?

From: RobB <ferndoc9@hotmail.com>
Date: Tue Jun 21 2005 - 19:28:38 CEST

jklimek@gmail.com wrote:
> I have something like this:
>
> <table name="test" id="test">
> <tr>
> <td><input type="button" onClick="showMeParentTableName();"></td>
> </tr>
> </table>
>
>
> How can I have my input button show me the table of it's parent table?
> (eg. "test" in this case)

"name" is not a valid attribute of an HTML table. Use id. Pass the
input object to the function.

<html>
<head>
<title>untitled</title>
<script type="text/javascript">

function getTableId(obj)
{
 while (obj && !/table/i.test(obj.nodeName))
 {
  obj = obj.parentNode;
 }
 return obj.id || '';
}

</script>
</head>
<body>
<table border="1" id="test1">
<tbody>
<tr>
<td>
<input
type="button"
value="test 1"
onclick="alert(getTableId(this))">
</td>
</tr>
</tbody>
</table>
<table border="1" id="test2">
<tbody>
<tr>
<td>
<input
type="button"
value="test 2"
onclick="alert(getTableId(this))">
</td>
</tr>
</tbody>
</table>
</body>
</html>

michael elias wrote:
> try this;

(snip)

> function showMeParentTableName(oInput){
> this.value =
> this.parentNode.parentNode.parentNode.parentNode.name;
> }

'this' in the context of a global function points to *window*.

> notice that you must go up 4 levels in the object hierarchy;
>
> input -> cell -> row -> tablebody -> table
>
> this only applies to IE, firefox needs one less step up.

Any time you need to give involved instructions like the above, it's
time to use some program logic to *look for* the desired data.
Received on Tue Oct 18 02:47:04 2005