...

View Full Version : Problem with table col in DOM. Please help!



mqasem
07-23-2004, 10:51 PM
I created a table using DOM, and gave every cell an id. I also included col tags in the table, and gave them id's. Now, I can easily set the styles of the cells, but I can't seem to be able to change the style of the columns. The following is the code. I hope someone knows how to fix it.

Thank you

function Table(rows, columns, id)
{
var cellAttrib;
var colAttrib;
var myLocation = document.getElementById(id);
mytable = document.createElement("TABLE");
mytablebody = document.createElement("TBODY");
for (i = 0; i < columns; i++)
{
myColumn = document.createElement("COL");
myColumn.setAttribute("id","col_" + i);
mytablebody.appendChild(myColumn);
}
for(j = 0; j < rows; j++)
{
mycurrent_row = document.createElement("TR");
for(i = 0; i < columns; i++)
{
mycurrent_cell = document.createElement("TD");
currenttext = document.createTextNode("cell is row " + j + ", column " + i);
mycurrent_cell.appendChild(currenttext);
mycurrent_cell.setAttribute("id","cell_" + j + "_" + i);
mycurrent_row.appendChild(mycurrent_cell);
}
mytablebody.appendChild(mycurrent_row);
}
mytable.appendChild(mytablebody);
myLocation.appendChild(mytable);
mytable.setAttribute("border","2");
}

// Later in the HTML coding I include the following:
// This is how I call the above function to create a table.
// Don't worry about the GridID stuff.
// This is just where I place the table.

var myTable = new Table(10, 5, "GridID");

// This is where I attempt to change the color style of the col.
// No success here!

document.all['col_1'].style.color = 'red';

sad69
07-23-2004, 11:01 PM
I didn't know you could create a column using DOM... I didn't even know what it was! I thought a column was just a particular cell on every row..

I'm still not sure that you can refer to a column the way you are, and that's why I think there's no CSS that would work on that column that way. I think you would have to go through each TR and find the particular TD that you needed (to isolate that 'column') and set the style/className individually.

Either way, I've not seen anywhere that a column is created like this:
document.createElement("COL");

But rather this way:
document.createElement("COLUMN");

I don't know which way is correct, but that may be something..

Hope that helps,
Sadiq.

mqasem
07-23-2004, 11:09 PM
I haven't seen it myself, but I thought that any w3c tag would be DOM ready. This col tag can be included in a table the following way:

<table>
<col style="color:red">
<col style="color:blue">
<tr>
<td>cell11</td>
<td>cell12</td>
</tr>
<tr>
<td>cell21</td>
<td>cell22</td>
</tr>
</table>

the red color will affect cell11, and 21, and the blue color will affect cell12, and 22.

mqasem
07-24-2004, 08:39 AM
OK... I think I got closer to understanding DOM more. It turns out (I think) that DOM does not support col tag within the model, although it works if you create a col tag in a table using pure and unadulterated form of HTML. Let me explain how reached this conclustion (or semi-conclusion).

In my code I included:

<div id="myText">
</div>
<script>
document.all['myText'].innerText = document.body.innerHTML;
</script>

What this did was display the HTML coding that was generated by the script. It turned out that the HTML code was formatted correctly. Why, then, didn't it work?

To find out if the HTML code works I copied it into another file, and loaded it into the browser. It worked!! That means that I can't script col into being, but I can certainly HTML it.

I guess I would have to work with cells if I wanted to control my columns through DOM.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum