Vortex Cortex
08-08-2003, 01:09 PM
I'm creating a menu using a table. The cells contain images. My problem is: when I generate the table using the DOM, setting the table attributes (cellspacing and cellpadding) does not produce the desired results.
<html>
<head><title>Testing</title></head>
<body>
<!-- Desired output from below javascript --/>
<div>
<table cellpadding="0px" cellspacing="0px" id="table1">
<tbody>
<tr>
<td><img src="images/home0.gif"></td>
</tr>
<tr>
<td><img src="images/about0.gif"/></td>
</tr>
</tbody>
</table>
</div>
<br/>
<div id="table2">
<!-- A div to hold the dynamic table --/>
</div>
<script language="javascript" type="text/javascript">
var image = document.createElement( "img" );
image.setAttribute( "src", "images/home0.gif" );
var cell = document.createElement( "td" );
cell.appendChild( image );
var row = document.createElement( "tr" );
row.appendChild( cell );
var tableBody = document.createElement( "tbody" );
tableBody.appendChild( row );
image = document.createElement( "img" );
image.setAttribute( "src", "images/about0.gif" );
cell = document.createElement( "td" );
cell.appendChild( image );
row = document.createElement( "tr" );
row.appendChild( cell );
tableBody.appendChild( row );
var table = document.createElement( "table" );
table.setAttribute( "cellpadding", "0px" );
table.setAttribute( "cellspacing", "0px" );
table.appendChild( tableBody );
document.getElementById( "table2" ).appendChild( table );
</script>
</body>
</html>
The first table looks fine. There is no space between the cells. However, the second table's cells have space between them (even if they were placed on the same row). I can produce the desired results if I "hard-code" an empty table element within the div, and set the table's attributes within the hard-coded element.
<div>
<table id="table2" cellpadding="0px" cellspacing="0px"></table>
</div>
...
<script ... >
...
document.getElementById( "table2" ).appendChild( tableBody );
</script>
But this defeats the purpose: It's important that I be able to create the entire table, and append it to any other element.
My guess is that I'm not setting the table attributes correctly.
Any suggestions would be greatly appreciated.
<html>
<head><title>Testing</title></head>
<body>
<!-- Desired output from below javascript --/>
<div>
<table cellpadding="0px" cellspacing="0px" id="table1">
<tbody>
<tr>
<td><img src="images/home0.gif"></td>
</tr>
<tr>
<td><img src="images/about0.gif"/></td>
</tr>
</tbody>
</table>
</div>
<br/>
<div id="table2">
<!-- A div to hold the dynamic table --/>
</div>
<script language="javascript" type="text/javascript">
var image = document.createElement( "img" );
image.setAttribute( "src", "images/home0.gif" );
var cell = document.createElement( "td" );
cell.appendChild( image );
var row = document.createElement( "tr" );
row.appendChild( cell );
var tableBody = document.createElement( "tbody" );
tableBody.appendChild( row );
image = document.createElement( "img" );
image.setAttribute( "src", "images/about0.gif" );
cell = document.createElement( "td" );
cell.appendChild( image );
row = document.createElement( "tr" );
row.appendChild( cell );
tableBody.appendChild( row );
var table = document.createElement( "table" );
table.setAttribute( "cellpadding", "0px" );
table.setAttribute( "cellspacing", "0px" );
table.appendChild( tableBody );
document.getElementById( "table2" ).appendChild( table );
</script>
</body>
</html>
The first table looks fine. There is no space between the cells. However, the second table's cells have space between them (even if they were placed on the same row). I can produce the desired results if I "hard-code" an empty table element within the div, and set the table's attributes within the hard-coded element.
<div>
<table id="table2" cellpadding="0px" cellspacing="0px"></table>
</div>
...
<script ... >
...
document.getElementById( "table2" ).appendChild( tableBody );
</script>
But this defeats the purpose: It's important that I be able to create the entire table, and append it to any other element.
My guess is that I'm not setting the table attributes correctly.
Any suggestions would be greatly appreciated.