Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    accessing parentNode

    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>

  • #2
    Regular Coder
    Join Date
    Aug 2002
    Location
    S„o Paulo, Brazil
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I don't have a good explanation why, but I think this will work:
    Code:
    <html>
    <head>
    </head>
    <body>
    <table>
    <tr> 
    <td class="c1">
    <a href="#" onclick="this.parentNode.className='c2'">test</a>
    </td> 
    </tr>
    </table>
    </body>
    </html>
    /Daniel

  • #3
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #4
    New Coder
    Join Date
    Oct 2002
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you both! it worked!

  • #5
    Regular Coder
    Join Date
    Jul 2003
    Location
    Massachusetts
    Posts
    472
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Master Coder
    Join Date
    Feb 2003
    Location
    UmeŚ, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    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.)
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards

  • #7
    Senior Coder
    Join Date
    Jun 2002
    Location
    near Oswestry
    Posts
    4,508
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    "Why bother with accessibility? ... Because deep down you know that the web is attractive to people who aren't exactly like you." - Joe Clark


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •