This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
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
Author

trying to get the parent of an element

Ram Thirumangalam
Greenhorn

Joined: Aug 28, 2003
Posts: 20
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

Joined: Nov 08, 2001
Posts: 15376
    
    6
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

Joined: Feb 02, 2001
Posts: 413
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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: trying to get the parent of an element
 
Similar Threads
auto-generate new ticket on each page
jQuery not working on page
jquery accordion is causing jquery treeview picker to breakup
Creating CSS class at runtime
Problem when submit a form with HTTPUnit