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

J

jklimek

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)
 
M

michael elias

try this;

<table name="test" id="test">
<tr>
<td><input type="button"
onClick="showMeParentTableName(this);"></td>
</tr>
</table>

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

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.
 
M

Mick White

How can I have my input button show me the table of it's parent table?
(eg. "test" in this case)
....
<td><input type="button" onClick="climbTo(this,'table')"></td>
....

<script type="text/javascript">
function climbTo(element,tagname){
var pa=element.parentNode;
while(pa.tagName.toLowerCase()!=tagname.toLowerCase()){
pa=pa.parentNode;
}
alert(pa.name)
}
</script>

Mick
 
R

RobB

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 said:
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.
 
G

Grant Wagner

Mick White said:
...
<td><input type="button" onClick="climbTo(this,'table')"></td>
...

<script type="text/javascript">
function climbTo(element,tagname){
var pa=element.parentNode;
while(pa.tagName.toLowerCase()!=tagname.toLowerCase()){
pa=pa.parentNode;
}
alert(pa.name)
}
</script>

If this function can't find the tagName it's looking for, it eventually
generates an error:

<span id="blah"></span>
<script type="text/javascript">
function climbTo(element,tagname){
var pa=element.parentNode;
while(pa.tagName.toLowerCase()!=tagname.toLowerCase()){
pa=pa.parentNode;
}
alert(pa.name)
}
climbTo(document.getElementById('blah'), 'table');
</script>

Something like this might be better:

function climbTo(element, tagname) {
tagname = tagname.toLowerCase();
var parentElement;
while((element = element.parentNode) && !parentElement) {
if (element.tagName.toLowerCase() == tagname) {
parentElement = element;
}
}

// return parentElement;
if (parentElement) {
alert(parentElement.id);
}
}
 
T

Thomas 'PointedEars' Lahn

Grant said:
If this function can't find the tagName it's looking for, it
eventually generates an error:
[...]
Something like this might be better:

function climbTo(element, tagname) {
tagname = tagname.toLowerCase();
var parentElement;
while((element = element.parentNode) && !parentElement) {
if (element.tagName.toLowerCase() == tagname) {
parentElement = element;
}
}

// return parentElement;
if (parentElement) {
alert(parentElement.id);
}
}

I'd prefer RegExps here:

function climbTo(element, tagname)
{
var rx = new RegExp(tagname, "i");
var parentElement;
while ((element = element.parentNode) && !parentElement)
{
if (element.tagName.test(rx))
{
parentElement = element;
}
}

// return parentElement;
if (parentElement)
{
alert(parentElement.id);
}
}


PointedEars
 
L

Lasse Reichstein Nielsen

Thomas 'PointedEars' Lahn said:
I'd prefer RegExps here:

function climbTo(element, tagname)
{
var rx = new RegExp(tagname, "i");

You should at least anchor the match, i.e.,
var rx = new RegExp("^"+tagname+"$", "i");

Otherwise tagnames that are substrings of other tag names can
give incorrect matches (which will probably not happen for valid
HTML, but might as well be safe).

....
if (element.tagName.test(rx))
{
parentElement = element;
} ....
if (parentElement)

I'm not sure the original poster had thought this through enough for
it to be generalized. This code will find the outermost enclosing
element matching the tag name, not the innermost one. That might be
desired behavior in this case, but in general, I'd more likely be
needing the nearest enclosing element.

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top