gr******@gmail.com wrote:
Please quote what you are replying to, trim what you aren't.
I've found the only reliable way to do this (for me) is to go
tableOb=document.getElementById('theTable');
rowOb=tableOb.rows[tableOb.length];
tableOb.removeChild(rowOb);
That won't work because tableOb.rows[tableOb.length] will always be one
greater than the highest index, i.e. the index of the last row. The
indexes start at zero, for a table with 3 rows the highest index will be
2 (the rows have indexes: 0, 1, 2).
(or is that rowOb=tableOb.rows[tableOb.length-1]; ?)
As per my earlier reply: if menuTable is a reference to a table, to
remove the last row:
var t = menuTable.rows.length;
if (t){
menuTable.deleteRow(t - 1);
}
That uses the table's rows collection which contains all the rows in the
table. If you have a thead or tfoot, you may not wish to remove rows
from them so you'll need to use the tbody element and delete rows from
that. thead, tfoot and tboby elements all have a rows collection, they
implement the HTMLTableSection interface:
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-67417573>
If you have thead and tfoot elements, you may wish to use the table's
tbodys collection to get a reference to the last tbody, then remove the
last row from that. If you have multiple tbodys, you'll need a bit more
cleverness again.
had a similar issue with a drag and drop table rows system..
document.body.removeChild(rowOb); might be more reliable? these things
are quirky
Not really. removeChild will only work for the (direct) children of a
node - it is intended as a general way of removing child nodes from an
element. It works with documents that implement the core DOM
interfaces, such as HTML and XML documents:
<URL:http://www.w3.org/TR/DOM-Level-2-Core/>
deleteRow belongs to the HTML table and table section interfaces, which
are specific to HTML-based documents and are intended to provide quicker
ways of dealing with HTML 4 and XHTML 1.0 DOM objects:
<URL:http://www.w3.org/TR/DOM-Level-2-HTML/>
A TR can't be a child of a body element, it must be a child of a table
section element - a thead, tfoot or tbody. If you don't explicitly code
a tbody element in your table, one will be inserted by the browser - the
tags are optional in the HTML source but a tbody element is mandatory in
the generated DOM. HTML 4 references:
HTML row groups (thead, tfood, tbody)
<URL:http://www.w3.org/TR/html4/struct/tables.html#edef-TBODY>
HTML table
<URL:http://www.w3.org/TR/html4/struct/tables.html#edef-TABLE>
--
Rob
Group FAQ: <URL:http://www.jibbering.com/faq/>