Click to See Complete Forum and Search --> : Creating tables using JavaScript?


Xeon
September 9th, 2002, 06:11 AM
Hi there! Can anyone gimme some sample code for creating a simple table in JavaScript?

I've tried something like:
document.tables.create();

but failed!!!!!! Help!!!!!!!

bharadwajrv
September 9th, 2002, 06:23 AM
hi,
use can try this code, but this will erase the existing content of the page..


document.write("<table width=600 border=1>");
document.write("<tr> <td> in side table </td> </tr>");
document.write("</table>");



venu

Zvona
September 9th, 2002, 08:09 AM
Advanced scripting with DOM (Document Object Model) for DOM Compliant browsers :

<script type="text/javascript">
<!--
var oTable = document.createElement('table');
var oTRow = document.createElement('tr');
var oTData = document.createElement('td');
var sText = document.createTextNode("Hello World!");

oTData.appendChild(sText);
oTRow.appendChild(oTData);
oTable.appendChild(oTRow);

window.onload = function()
{
document.body.appendChild(oTable);
}
And there you have a document with structure :

<body>
<table>
<tbody>
<tr>
<td>Hello World!</td>
</tr>
</tbody>
</table>
</body>
Now, you can reference to table's nodes with scripting by using objects defined. For instance :
oTable.style.border = "2px outset #80C0F0";
equals to :
<table style="border:2px outset #80C0F0;">

Xeon
September 9th, 2002, 10:50 AM
Thanks a lot, Zvona and bharadwajrv!!!!!!! :D:):D