Quick way to find all tds?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • bill

    #1

    Quick way to find all tds?



    I'm relatively new to JS so this question is very naive. Is there
    a way to quickly get an array of all the HTML elements of a given
    class, say all the <td ...>, or all the <font ...> elements? The
    only way I can think of involves a tedious tree traversal.

    Thanks!

    bill
  • Michael Winter

    #2
    Re: Quick way to find all tds?

    On Thu, 12 Feb 2004 03:29:39 +0000 (UTC), bill <bill_knight2@y ahoo.com>
    wrote:
    [color=blue]
    > I'm relatively new to JS so this question is very naive. Is there
    > a way to quickly get an array of all the HTML elements of a given
    > class, say all the <td ...>, or all the <font ...> elements? The
    > only way I can think of involves a tedious tree traversal.[/color]

    document.getEle mentsByTagName( ), a DOM method can be used to retrieve
    elements by element name. The DHTML equivalent is document.all.ta gs().
    Note that both can be used with any HTML object (such as DIV, TABLE, BODY,
    and FORM references) so the browser won't have to traverse the entire tree.

    Before using either, you should test for their support:

    var tableCells = null;

    if( document.getEle mentsByTagName ) {
    tableCells = document.getEle mentsByTagName( 'td');
    } else if( document.all && document.all.ta gs ) {
    tableCells = document.all.ta gs('td');
    }
    if( tableCells ) {
    // tableCells is now a collection that contains
    // all table cell elements in the document.
    }

    Mike

    --
    Michael Winter
    M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

    Comment

    • bill

      #3
      Re: Quick way to find all tds?




      That's great. Thanks!

      bill

      Comment

      • Michael Winter

        #4
        Re: Quick way to find all tds?

        On Thu, 12 Feb 2004 09:39:38 GMT, Michael Winter
        <M.Winter@bluey onder.co.invali d> wrote:

        [snip]
        [color=blue]
        > document.getEle mentsByTagName( ), a DOM method can be used to retrieve
        > elements by element name. The DHTML equivalent is document.all.ta gs().
        > Note that both can be used with any HTML object (such as DIV, TABLE,
        > BODY, and FORM references) so the browser won't have to traverse the
        > entire tree.[/color]

        [snip]

        After re-reading that last sentence, I came to the conclusion that it's
        rather lacking in detail. What I meant was that getElementsByTa gName() is
        not just a method of the document object, but of all HTML elements. This
        allows you to get all of the table cells of a specific table, for example.
        You would first get a reference to the table (using getElementById( ) or
        similar), then call getElementsByTa gName() from that object.

        Using an example similar to the last one:

        var tableRef = null;

        if( document.getEle mentById ) {
        tableRef = document.getEle mentById('myTab le');
        } else if( document.all ) {
        tableRef = document.all['myTable'];
        }
        if( tableRef ) {
        var tableCells = null;

        if( tableRef.getEle mentsByTagName ) {
        tableCells = tableRef.getEle mentsByTagName( 'td');
        } else if( tableRef.all && tableRef.all.ta gs ) {
        tableCells = tableRef.all.ta gs('td');
        }
        if( tableCells ) {
        // tableCells is now a collection that contains
        // all table cell elements in the table, myTable
        // (referenced by tableRef).
        }
        }

        Mike

        --
        Michael Winter
        M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

        Comment

        Working...