Jump to content

Getting the child anchor <a> element of a table cell <td> ..


Adam

Recommended Posts

Pretty much says it all in the name. I can't simply use getElementById() because I won't know what the IDs will be. I've tried using childNodes but with no look so far - also tried firstChild. This doesn't require compliance with IE as I've already achieved what I want in IE.. Can anybody offer any help?

 

Cheers! Adam

Link to comment
Share on other sites

This book: Pro Javascript Techniques, has a lot of good traversing functions.

http://jspro.org/code/ [Download All Code]

 

I'm missing one function from here that you need: prevSibling(), but I don't want to search through his code to find it.

 

Here are the important parts, you just need to search his code for "prevSibling"() function, and copy it.  Then you should be able to use these in combination to get what you want.

 

function prev( elem ) {
    do {
        elem = elem.previousSibling;
    } while ( elem && elem.nodeType != 1 );
    return elem;
}
//-------------------------------------------------------------------------------
function next( elem ) {
    do {
        elem = elem.nextSibling;
    } while ( elem && elem.nodeType != 1 );
    return elem;
}
//-------------------------------------------------------------------------------
function first( elem ) {
    elem = elem.firstChild;
    return elem && elem.nodeType != 1 ?
        nextSibling( elem ) : elem;
}
//-------------------------------------------------------------------------------
function last( elem ) {
    elem = elem.lastChild;
    return elem && elem.nodeType != 1 ?
        prevSibling( elem ) : elem;
}
//-------------------------------------------------------------------------------
function parent( elem, num ) {
    num = num || 1;
    for ( var i = 0; i < num; i++ )
        if ( elem != null ) elem = elem.parentNode;
    return elem;
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.