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

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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.