Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trying to get the parent of an element

 
Ram Thirumangalam
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
I am having some issues while trying to get the parent of an <a> element in javascript. The <td> element of my table contains an 'IMG' tag followed by 'A'(href) tag. On clicking the A element tag text, i execute a javascript passing 'this' as its parameter and in the script, I try to access the parent of the A element tag by using this.parentNode. It should return me the TD elemet tag. However ,it returns an undefined object eventhough upon displaying the childnodes of the TD element tag, the 'A' element is listed as one of its children.
Does anyone have an idea why it doesnt return the parent?
Please help.
Here is the sample code.
<html>
<head>
...inline style sheet
<script>
function changeclass(current)
{
current.parentNode.className=yyy;
}
</script>
</head>
<body>
<table>
<tr>
<td class=xxx>
<img src='test.gif'>
<a href="javascript:changeclass(this)test.html'>click here</a>
</td>
</tr>
</table>
</body>
Thanks
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not remember the reason but for some reason the link does not conform to the parentNode object.
SO
a fix:
 
Yuriy Fuksenko
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can not pass "this" through href.
Here the code:
<a href="#" onclick="changeclass(this)">click it</a>
or
<a href="#" onclick="this.parentNode.className='aaaaa'">click it</a>
[ February 06, 2004: Message edited by: Yuriy Fuksenko ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic