...

View Full Version : accessing parentNode



hongr
01-16-2004, 06:54 PM
I am trying to change the class of the td when clicking on "test", but got an "undefined" error. What's the correct syntax to reference the parentNode or the node above the current one? thanks.

<html>
<head>
</head>
<body>
<table>
<tr>
<td class="c1">
<a href="javascript:this.parentNode.className='c2'">test</a>
</td>
</tr>
</table>
</body>
</html>

Danne
01-16-2004, 06:59 PM
I don't have a good explanation why, but I think this will work:


<html>
<head>
</head>
<body>
<table>
<tr>
<td class="c1">
<a href="#" onclick="this.parentNode.className='c2'">test</a>
</td>
</tr>
</table>
</body>
</html>

liorean
01-16-2004, 07:06 PM
This is a pretty simple problem, really. The browser executes the comment in the context of the page it is trying to go to - and the link does not exist on the target page, which is why it becomes undefined. If you ended the address with a void(0) instead, you'd return nothing and therefore not try to go anywhere - the context will now be the current page.

This is misuse of the javascript pseudo protocol, though. A link references a URI, and pseudo protocols are just addresses that look like URIs, they aren't true URIs. Instead, you should use the JavaScript in an event handler such as onclick. That both solves the context problem and makes for neater, more standards compliant code.

hongr
01-16-2004, 07:14 PM
thank you both! it worked!

fredmv
01-16-2004, 07:30 PM
Originally posted by liorean
This is misuse of the javascript pseudo protocol, though. I completely agree, and I've seen way too many people misuse it. Would you agree that the only real use of it is for bookmarklets? That's how I've always thought of it for the most part.

liorean
01-16-2004, 07:35 PM
In fact, that is pretty much the only valid reason for using them, Unless you count the really exotic things you could do, such as encode an image in a javascript pseudo protocol address, so that it actually displays. (Yes, it can be done. But why you would do that if you can use the data URI protocol to do the same more effectively beats me.)

brothercake
01-17-2004, 03:03 AM
You can use it to call a javascript function from ActionScript, by using the javascript: pseudo-protocol within getURL; don't think there's another way of doing that.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum