Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Change table cell text alignment?

Reply
Thread Tools

Change table cell text alignment?

 
 
harry
Guest
Posts: n/a
 
      01-19-2005
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


 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-19-2005


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/
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      01-19-2005
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
 
Reply With Quote
 
Spats30
Guest
Posts: n/a
 
      01-21-2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
form input text in table; how to fill table cell space sdf HTML 3 12-06-2007 03:29 PM
How to dynamically change text in a table's TD cell? coolsti Javascript 5 06-02-2005 05:52 AM
How to set and highlight text in JTable cell on selection and/or invoke cell editor directly. news.rcn.com Java 1 07-12-2004 10:22 PM
How do I have a table cell fetch another page to display within the cell? Phillip Roncoroni HTML 14 04-05-2004 05:58 PM
Table Cell Colour Change Blindsya HTML 1 09-17-2003 10:03 PM



Advertisments