Meaningless Drivel is fun!
The moose likes HTML, CSS and JavaScript and the fly likes trying to get the parent of an element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "trying to get the parent of an element" Watch "trying to get the parent of an element" New topic

trying to get the parent of an element

Ram Thirumangalam

Joined: Aug 28, 2003
Posts: 20
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.
...inline style sheet
function changeclass(current)
<td class=xxx>
<img src='test.gif'>
<a href="javascript:changeclass(this)test.html'>click here</a>
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
I do not remember the reason but for some reason the link does not conform to the parentNode object.
a fix:
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
you can not pass "this" through href.
Here the code:
<a href="#" onclick="changeclass(this)">click it</a>
<a href="#" onclick="this.parentNode.className='aaaaa'">click it</a>
[ February 06, 2004: Message edited by: Yuriy Fuksenko ]
I agree. Here's the link:
subject: trying to get the parent of an element
It's not a secret anymore!