[web] How do I create a table object using DOM?

Started by
4 comments, last by Tesseract 18 years ago
Aside from creating a table in HTML and then using document.getElementById() to get a table object, is there a way I can create a table object that is empty? For example, is there a Constructor where I can do this: MyTable= new Table();
-----------------------------Download my real time 3D RPG.
Advertisement
var tbl = document.createElement('table');

Search for appendChild() to see how you can build up your table (with TR's and TD's), and then attach the table to your page.

Note, I'm pretty sure you'll find you can't your rows directly to your table object (in some browsers--I think it's a bug). Instead you have to add them to a TBODY element, which you can then add to the table.
Thanks.
-----------------------------Download my real time 3D RPG.
Quote:Original post by Anonymous Poster
Note, I'm pretty sure you'll find you can't your rows directly to your table object (in some browsers--I think it's a bug). Instead you have to add them to a TBODY element, which you can then add to the table.


If memory serves, that's actually following the standard. THEAD, TBODY, and TFOOT are supposed to be what TRs go under, not the table itself.
Once you have created a table, you can use table DOM functions such as insertCell() and insertRow(). I used these functions in a demo but it doesn't seem to be much easier or faster than appendChild(). See http://members.optusnet.com.au/khsoh/0005.html and look at the &#106avascript code if you are interested.<br><br>-- <br>
Kam-Hung Sohhttp://softwaresalariman.blogspot.com
Some of the DOM table manipulation methods are not as cross-browser accessable as one would expect. Here is a good reference to use.

This topic is closed to new replies.

Advertisement