• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trying to get the parent of an element

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Your buns are mine! But you can have this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic