The code you've provided ought to be fine (I haven't bug checked it, but it looks okay), but sadly adding rows to a table is notoriously problematic. There are various methods of doing it, but most of them fail to work in at least one of the major browsers. Some browsers are happiest if you simply use innerHTML to append your new rows; others prefer object model code, as you've written; and others just don't like changing a table at all once it's been displayed. You didn't say which browser you're using, but if you're doing this sort of thing it's a good plan to test in as many as possible anyway.
If you're dealing with a known list of items that can be added an removed, my advice would be simply to build the table with them all already in place, but the ones you don't want to display set to invisible using Javascript to change their CSS style. It is a lot simpler to manage that way.
On the other hand, if you're dealing with adding an unknown number of items to the table, you may have to take a different approach. You might try using <div>s and CSS to simulate the table layout, which will make it a lot easier to manipulate in javascript, but harder to get right in terms of layout, or you could simply rebuild the entire table each time you need to add or remove a row. You could also write browser-specific code to make the table work right for whichever browser is being used, but with users spoofing other browsers, etc, that's not really my preferred solution. There are a few other options you could try, but in the end, whatever approach you decide, it isn't going to be as simple as it should be.
|