468,471 Members | 1,622 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,471 developers. It's quick & easy.

Change table cell text alignment?

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
Jul 23 '05 #1
3 15713


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/
Jul 23 '05 #2
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
Jul 23 '05 #3
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.

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Pete Kipe | last post: by
1 post views Thread by Thanks | last post: by
reply views Thread by Mark A. Sam | last post: by
2 posts views Thread by charles-brewster | last post: by
3 posts views Thread by mridul | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.