Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get number of columns in table

Status
Not open for further replies.

cylly

Programmer
Oct 17, 2002
23
CA
How do I retrieve the number of columns in a table? The table is loaded dynamicaly so the number of columns will vary depending on what was selected on a previous page.
 
how about...

// get the table by the id ById
var tableElEM = document.getElementById("tablename");
// select the cell tags in the table TagName
var tdIntbl = tableELE.getElementsByTagName("td");
// test the return output
alert(tdIntbl);

Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
No such luck. I am getting [object] for output. Any other options?
 
sorry I forgot to change the alert to access the length
try this

// get the table by the id ById
var tableELEM = document.getElementById("tablename");
// select the cell tags in the table TagName
var tdIntbl = tableELEM.getElementsByTagName("td");
// test the return output
alert(tdIntbl.length);

also had some typos in my fast typing spree in the naming. apologies.
also to mention you need a id in the table to access the td's this way Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
if you build this table dynamicaly (with ASP for example ).

In your loop increase a variable (for example X) each time you write a 'td' or something else
after write an input like this
<input type=hidden id=TD_NUMBER value=&quot;<%=X%>&quot;>

in your javascript's function u can initialize a variable like this :

var TDS = document.getElementById(&quot;TD_NUMBER&quot;).value;
 
How do I increase the variable each time I write a 'td'?

BTW - Thanks onpnt. Your suggestion worked wonderfully. I get the number of cells. I can also get the number of rows and then caluclate the number of columns.

I am not sure yet if I will use onpnt's suggestion or 2ni's suggestion. Thank you both.
 
each time you write a cell to the page you are reloading the page, so if you have the function in a option explicit or global in javascript terms to run onLoad you will populate the var each time with the new number Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
Well, hate to disagree or point out anything (well, no, no I don't :p), BUT....

If you build this table dynamically client-side using the document object model to create new rows and cells and add them to the table, the page won't reload :)

When it comes to the number of columns, what you may want to do is loop through all the rows and get the largest column number, so you can be sure you have the right value:
Code:
<table>
   <tr>
      <td colspan=&quot;2&quot;></td>
      <td></td>
   </tr>
   <tr>
      <td></td>
      <td colspan=&quot;2&quot;></td>
   </tr>
   <tr>
      <td></td>
      <td></td>
      <td></td>
   </tr>
</table>
How many columns does the above table have? 7/3?

Just a thought :)

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
wait a minute..
I'll explain myself, as usual Tarwn makes me [smile]

I was coming from a server side aspect of things in dynamicaly adding records etc.. to (rows, cells) in the page.

I guess seeing as there was no argument from 2ni with the use of server side code in the example I took it for granted that that might be the case.

On the client side of things, of course the page will not reload unless you reload it causing no change unless you specifically run a function everytime you add cells (which you could do as well), but then if you are doing everything client side then as you reload it you change everything back to to the default way it. Just a suggestion: faq183-874
admin@onpntwebdesigns.com
 
Sorry about that, I saw the element references above and though I should point it out, just me bein me and keeping you on your toes :)

Here's your guarantee -
smiley_on_tredmil.gif


-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top