473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Width of rendered table cell

1
Is it possible through javascript to determine the width of a rendered table cell? I have two adjacent tables, one on top of the other. Both tables have the same number of columns, because the content of each column in the top table is related to the its corresponding column in the bottom table (I needed to use two tables because of the UI I had to design). Depending on the size of the numbers in the bottom table's columns, each individual column will vary in width. But to ensure the corresponding top table's column match, I need to resize the top table's columns after the bottom table has been rendered. I've attached a picture to illustrate.

If in javascript I obtain a column's TD element, the width, innerWidth or offsetWidth properties are either null or a value that doesn't correspond to the true width of the column.

Does anyone know how to get the true rendered width value of a table cell?

Thanks in advance
Attached Images
File Type: jpg TableAnnotated.JPG (33.7 KB, 1485 views)
Dec 5 '06 #1
2 24465
iam_clint
1,208 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. <script>
  2. function sizecolumns() {
  3. if (document.getElementById("5").style.width < document.getElementById("1").offsetWidth) {
  4.     document.getElementById("5").style.width = document.getElementById("1").offsetWidth - 2;
  5. } else {
  6.     document.getElementById("1").offsetWidth = document.getElementById("5").style.width - 2;
  7. }
  8.  
  9. if (document.getElementById("2").style.width < document.getElementById("6").offsetWidth) {
  10.     document.getElementById("2").style.width = document.getElementById("6").offsetWidth - 2;
  11. } else {
  12.     document.getElementById("6").offsetWidth = document.getElementById("2").style.width - 2;
  13. }
  14.  
  15. if (document.getElementById("3").style.width < document.getElementById("7").offsetWidth) {
  16.     document.getElementById("3").style.width = document.getElementById("7").offsetWidth - 2;
  17. } else {
  18.     document.getElementById("7").offsetWidth = document.getElementById("3").style.width - 2;
  19. }
  20.  
  21. if (document.getElementById("8").style.width < document.getElementById("4").offsetWidth) {
  22.     document.getElementById("8").style.width = document.getElementById("4").offsetWidth - 2;
  23. } else {
  24.     document.getElementById("4").style.width = document.getElementById("8").offsetWidth - 2;
  25. }
  26. }
  27. </script>
  28. <table border="1">
  29. <tr>
  30. <td id="1">This is a test<td id="2">to<td id="3">size<td id="4">columns
  31. </table>
  32. <table border="1">
  33. <tr>
  34. <td id="5">based on<td id="6">how big its corresponding<td id="7">cell<td id="8">is
  35. </table>
  36. <input type="button" value="size columns" onclick="sizecolumns();">
  37.  
this is pretty close but not 100% accurate just trying to give you an example
Dec 5 '06 #2
Building on the previous post, this here works quite well...
Expand|Select|Wrap|Line Numbers
  1.  
  2.  <table border >
  3.           <tr><td id=hd1>Column 1</td><td id=hd2>Column 2</td><td id=hd5>Column 5</td></tr>
  4.     </table>
  5.     <table border>
  6.        <tr><td id=col1>&nbsp;</td><td id=col2>&nbsp;</td><td id=col5>&nbsp;</td></tr>
  7.     <tr><td>Afr.</td><td>Swallow w/Coconut</td><td>45/min</td></tr>
  8.     <tr><td>Eur.</td><td>Swallow w/Coconut</td><td>47/min</td></tr>
  9.     </table>
  10.     <script language="javascript" type="text/javascript">
  11.       var w1,w2,w5;
  12.       var hd1 = document.getElementById("hd1");
  13.       var hd2 = document.getElementById("hd2");
  14.       var hd5 = document.getElementById("hd5");
  15.       var col1 = document.getElementById("col1");
  16.       var col2 = document.getElementById("col2");
  17.       var col5 = document.getElementById("col5");
  18.       w1 = hd1.offsetWidth > col1.offsetWidth ? hd1.offsetWidth : col1.offsetWidth;
  19.       w2 = hd2.offsetWidth > col2.offsetWidth ? hd2.offsetWidth : col2.offsetWidth;
  20.       w5 = hd5.offsetWidth > col5.offsetWidth ? hd5.offsetWidth : col5.offsetWidth;
  21.  
  22.       hd1.width = w1; col1.width = w1;
  23.       hd2.width = w2; col2.width = w2;
  24.       hd5.width = w5; col5.width = w5;      
  25.     </script>
  26.  
  27.  
Nov 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Graham J | last post by:
Apologies for the partial post that mysteriously escaped as I was composing. I shall try again. For many years I have been happily and successfully coding tables such that the WIDTH specified...
23
by: MattB | last post by:
Hello please help, I have a table cell with a div in it. The div has a width of 300px. but when it is rendered it puts extra space into the table cell. Here's the style <style>...
4
by: N. Demos | last post by:
The following code renders as intended in IE (A TABLE, with cells of fixed width and height, inside of a DIV with fixed width and height and overflow set to hidden.) In Firefox, the table cells...
1
by: martin | last post by:
Hi, I have a datagrid that contains 3 colums. This is rendered to the page fine, except that I would like to be able to control the width of each table cell of the datagrid I have the following...
18
by: chimalus | last post by:
I am using a table with no column widths specified, letting the table layout manager do its thing for figuring out the column widths, and this works just fine. Now I want to make the table...
2
by: todd.freed | last post by:
Hi All, I am using the <asp: Calendar> object. In the OnDayRender event handler I am putting text into certain day boxes, and leaving others blank. The effect is that some months have no...
0
by: jwwishart | last post by:
Hi, Weve been doing this sort of thing frequently in ASP.NET but now were working on a project in ASP.NET 2.0 and were having this very strange problem. With the following XHTML... <table...
1
by: AndrewF | last post by:
Hi all, I've been trying to google this bug for a few hours and haven't turned up anything which leads me to believe the way I'm framing the question is the way someone else has. Anyway...
1
by: =?Utf-8?B?d2NsYXJrZTEwMQ==?= | last post by:
Hello all, I've created the following ASP.NET code sample which, when rendered, displays a 100% textbox within a single cell table. The problem is that Internet Explorer (6 or 7) truncates the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.