473,799 Members | 3,740 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16204


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.r ows.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.getEl ementById) {
tableElement = document.getEle mentById('table 1');
tableCell = document.getEle mentById('cell1 ');
// then do above check here
}
but of course the DOM offers various other possibilities (e.g.
document.getEle mentsByTagName( '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.r ows.length > 200) {
tableCell.align = 'right';


You can also play with the style object:

if (tableElement.r ows.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
6183
by: Pete Kipe | last post by:
I'm not a JavaScript programmer...but I'm trying to put together a simple menu system for a new website and need a little help. I have the following script: <SCRIPT language=javascript> <!-- Hide JavaScript from Java-Impaired Browsers function NavRollOver(oTd) {if (!oTd.contains(event.fromElement)) {oTd.bgColor="#CC0000";}} function NavRollOut(oTd) {if (!oTd.contains(event.toElement))
1
3554
by: Thanks | last post by:
I have a routine that is called on Page_Init. It retrieves folder records from a database which I display as Link Buttons in a table cell. I set the table cell's bgcolor to a default color (say black for example). I am dynamically creating the LinkButton controls and adding them into the table cell and I've also hooked up an event handler for each LinkButton's Click event. This all works fine. Now in the Link Button's Click event...
0
1051
by: Mark A. Sam | last post by:
Hello, Whenever I add a table, the text position of the cells is centered. I would like the default to be left-top. Is there a way to set this? Thank you and God Bless, Mark A. Sam
2
13937
by: charles-brewster | last post by:
I'm trying to write a simple JavaScript function which will use a button to copy table cell data into a form input text box as the "value" attribute. The following is intended to test the function, but doesn't work. I'm new to this - previous JavaScript experience mostly copy & paste - could somebody please point to where I'm going wrong. ~~~~~~~~~~~~~~~~~~~~~~~~~ <head>
2
1874
by: charles-brewster | last post by:
I have progressed the copytext function test page a little since my original post here in the small hours this morning. It now opens a debugging window so I can try to figure out what it is or isn't doing. Current version of the test page: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ <head> <title>Testing JavaScript</title> <script language="JavaScript">
9
2613
by: Daniel Kabs | last post by:
Hello there again! On my quest to remove layout tables in favor of CSS controlled elements, I am faced with quite a problem. A table cell that has no explicit width attribute set usually adapts its width to accommodate the content nicely. This is not the case when the content contains floats. I guess floats do not contribute to the table cell width as floated elements are taken out
2
16419
by: nino9stars | last post by:
Hello, I have just started messing with absolute positioning on webpages, and it definitely let's you do some creative things. Well, after much searching and help, I got the images I was using to overlap correctly. You can see it on this page: www.creativekaysjewelry.com The images overlap exactly how I wanted and in the right position
3
2493
by: mridul | last post by:
I have a html which contains a table. As the data comes dynamically so the table cells expand and contract accordingly.To stop it i have fixed the width of table cells(td). But in one table cell a large url (data which is not having any break) is coming and in this case constant width is not working and cell is expanding. This gives a ugly look in the table. Can you help me out so that this table cell doesn't expand and instead it starts a...
1
2446
by: asafok | last post by:
hi, i'm tring to get access to a cell inside a table that has the vlaue of an Email address but is not clickable. i tring to go to that cell and Wrap the inner text with <a href="mailto... tag so it would be clickable. i tried to use this code: x=document.getElementById('GridView1').childNodes.childNodes; but with no luck,for some resone i can't access the cell and it's text. need you help on doing so...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10485
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10231
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9073
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7565
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2938
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.