inserting a col tag in a table using DOM problem!!

M

MQ

I have been trying to insert a col tag using DOM but I can't seem to make it
work. In the following script I create a table with rows and columsn. I
tried to modify the column style, but doesn't seem to want to work. I tried
modifying the cell style and it works fine. Can anyone see what's wrong
with this code.

Thank you.


-------code-----------
<html>

<head>
<title>
</title>

<script language="Javascript">
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");
}
</script>

</head>

<body >
<div id="actionGridID">
</div>
<script language="Javascript">
<!--
var myTable = new Table(10, 5, "actionGridID");

document.all['col_1'].style.color = 'red';
-->
</script>



</body>

</html>


--
 
M

Martin Honnen

MQ said:
I have been trying to insert a col tag using DOM but I can't seem to make it
work. In the following script I create a table with rows and columsn. I
tried to modify the column style, but doesn't seem to want to work. I tried
modifying the cell style and it works fine.

With the following corrections made the script does set the
background-color of the column with Opera 7.50 and Mozilla 1.7, it
doesn't achieve that with IE 6 however:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>setting CSS style of col element</title>

<script type="text/javascript">
function Table(rows, columns, id)
{
var cellAttrib;
var colAttrib;
var myLocation = document.getElementById(id);
mytable = document.createElement("TABLE");
mytablebody = document.createElement("TBODY");
for (var i = 0; i < columns; i++)
{
myColumn = document.createElement("COL");
myColumn.setAttribute("id","col_" + i);
mytable.appendChild(myColumn);
}
for(var j = 0; j < rows; j++)
{
mycurrent_row = document.createElement("TR");
for(var 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");
}
</script>

</head>

<body >
<div id="actionGridID">
</div>
<script type="text/javascript">

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

document.getElementById('col_1').style.backgroundColor = 'red';
</script>



</body>

</html>

According to the CSS 2 specification at
http://www.w3.org/TR/CSS2/tables.html#q4
color is not one of the CSS properties that can be applied to columns
thus I have changed the example to use background-color. And the <col>
elements need to be a child of the table element not the tbody.
Mozilla I think needs to be in strict layout mode to apply column styles
thus I added the DOCTYPE declaration.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top