Change table cell text alignment?

Discussion in 'Javascript' started by harry, Jan 19, 2005.

  1. harry

    harry Guest

    I want to be able to change the text alignment within a table cell between
    "right" & "center" depending on how many rows are in the table.

    Is this possible in Javascript? - can't see how to do it!

    thanks

    harry
    harry, Jan 19, 2005
    #1
    1. Advertising

  2. harry wrote:

    > I want to be able to change the text alignment within a table cell between
    > "right" & "center" depending on how many rows are in the table.
    >
    > Is this possible in Javascript?


    In browsers that implement the W3C DOM HTML module as specified here
    <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-82915075>
    table cell element objects have property named align which you can read
    and set e.g.
    tableCell.align = 'right';
    Table element objects as documented here
    <http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-64060425>
    have a rows property whiich is a collection with a length property so
    you could check
    if (tableElement.rows.length > 200) {
    tableCell.align = 'right';
    }
    Getting a table or table cell elements can be done in various ways, by
    id e.g.
    <table id="table1">
    <tbody>
    <tr>
    <td id="cell1">...</td>
    ...
    ...
    </table>

    var tableElement, tableCell;
    if (document.getElementById) {
    tableElement = document.getElementById('table1');
    tableCell = document.getElementById('cell1');
    // then do above check here
    }
    but of course the DOM offers various other possibilities (e.g.
    document.getElementsByTagName('table')) to find elements.

    Support for that is in at least IE5+, Netscape 6+, Mozilla 1.x, Opera 7,
    hopefully in Safari and Konqueror too though I am not sure there I have
    ever tested to manipulate the align property of a table cell.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 19, 2005
    #2
    1. Advertising

  3. harry

    RobG Guest

    Martin Honnen wrote:
    [...]
    > if (tableElement.rows.length > 200) {
    > tableCell.align = 'right';


    You can also play with the style object:

    if (tableElement.rows.length > 200 && tableCell.style) {
    tableCell.style.textAlign = 'right';
    // more style manipulation
    }

    --
    Rob
    RobG, Jan 19, 2005
    #3
  4. harry

    Spats30 Guest

    You might also be able to put a div tag inside each cell with a class
    on it that will hold your content. Then, it would be really easy to
    dynamically change the class using javascript.
    Spats30, Jan 21, 2005
    #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. Phillip Roncoroni
    Replies:
    14
    Views:
    1,075
    Toby A Inkster
    Apr 5, 2004
  2. sdf
    Replies:
    3
    Views:
    1,885
  3. Eustace

    Table cell alignment

    Eustace, Sep 25, 2009, in forum: HTML
    Replies:
    9
    Views:
    717
    Travis Newbury
    Oct 1, 2009
  4. coolsti
    Replies:
    5
    Views:
    196
    coolsti
    Jun 2, 2005
  5. Replies:
    2
    Views:
    127
Loading...

Share This Page