Quick way to find all tds?

Discussion in 'Javascript' started by bill, Feb 12, 2004.

  1. bill

    bill Guest

    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
     
    bill, Feb 12, 2004
    #1
    1. Advertising

  2. On Thu, 12 Feb 2004 03:29:39 +0000 (UTC), bill <>
    wrote:

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


    document.getElementsByTagName(), a DOM method can be used to retrieve
    elements by element name. The DHTML equivalent is document.all.tags().
    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.getElementsByTagName ) {
    tableCells = document.getElementsByTagName('td');
    } else if( document.all && document.all.tags ) {
    tableCells = document.all.tags('td');
    }
    if( tableCells ) {
    // tableCells is now a collection that contains
    // all table cell elements in the document.
    }

    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Feb 12, 2004
    #2
    1. Advertising

  3. bill

    bill Guest

    That's great. Thanks!

    bill
     
    bill, Feb 12, 2004
    #3
  4. On Thu, 12 Feb 2004 09:39:38 GMT, Michael Winter
    <> wrote:

    [snip]

    > document.getElementsByTagName(), a DOM method can be used to retrieve
    > elements by element name. The DHTML equivalent is document.all.tags().
    > 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.


    [snip]

    After re-reading that last sentence, I came to the conclusion that it's
    rather lacking in detail. What I meant was that getElementsByTagName() 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 getElementsByTagName() from that object.

    Using an example similar to the last one:

    var tableRef = null;

    if( document.getElementById ) {
    tableRef = document.getElementById('myTable');
    } else if( document.all ) {
    tableRef = document.all['myTable'];
    }
    if( tableRef ) {
    var tableCells = null;

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

    Mike

    --
    Michael Winter
    d (replace ".invalid" with ".uk" to reply)
     
    Michael Winter, Feb 12, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DesignerX
    Replies:
    1
    Views:
    361
    David Wier
    Aug 13, 2003
  2. btopenworld

    display records in several TDs per TR?

    btopenworld, Nov 28, 2003, in forum: ASP General
    Replies:
    3
    Views:
    135
    Aaron Bertrand [MVP]
    Nov 30, 2003
  3. Damphyr
    Replies:
    4
    Views:
    323
    Damphyr
    Jul 26, 2003
  4. John Milton

    2 Tables, same width in tds?

    John Milton, Nov 7, 2004, in forum: Javascript
    Replies:
    2
    Views:
    115
  5. lofenee
    Replies:
    3
    Views:
    136
    Ben Bullock
    Apr 22, 2008
Loading...

Share This Page