I've got a simple question, I want to delete the last row in a table, I copied one function that deletes the checked one though:
function deleterow(tableID) {
try {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
for(var i=1; i<rowCount; i++) {
var row = table.rows[i];
var chkbox = row.cells[0].childNodes[0];
if(null != chkbox && true == chkbox.checked) {
table.deleteRow(row);
rowCount--;
i--;
}
}
}catch(e) {
alert(e);
}
}
How do I change it, so that it will delete only the last row each time it's called? Thank you all for the help! I really appreciate it!
the deleteRow function takes an index, pass it count - 1
.
http://jsfiddle.net/jbabey/HEDNZ/
function deleterow(tableID) {
var table = document.getElementById(tableID);
var rowCount = table.rows.length;
table.deleteRow(rowCount -1);
}
Why not just use:
function deleterow(tableID) {
var table = document.getElementById(tableID);
table.deleteRow(-1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With