View Full Version : Setting colspan at runtime

05-05-2008, 09:22 PM
I want to set the colspan of a header in a table to equal the number of columns in a datarow under it at runtime. Is this possible and how would I do it?


05-06-2008, 03:54 AM
Styles might do it. Of course, only a suggestion, since this won't work in IE6, which is still widely used (unfortunately).

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">


#tabl {
display: table;
width: 40&#37;;

div >div {
display: table-caption;
background: #ffa;
font-variant: small-caps;
font-size: 110%;
padding: 0.3em;
text-align: center;

div >span {
display: table-cell;
padding: 0.5em;
background: #aff;
border-left: 1px solid #000;

BR {
display: table-row;




<div id="tabl">




If on the other hand you need a table, you could make your caption a - caption - or a header - TH. Table -> TH -> TR . . td /td /TR, like that. And you could give the TH its own id and use getElementById, or if it's just the one table, a getElementsByTagName("th")[0] . You can then directly change its colspan, if you've counted the number of columns.

In IE6, though, remember that 'colspan' is a 'camel-case' name. So not, "colspan", but "colSpan".