Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
60
rated 0 times [  61] [ 1]  / answers: 1 / hits: 25573  / 12 Years ago, mon, may 21, 2012, 12:00:00

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!


More From » javascript

 Answers
2

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);
}

[#85453] Friday, May 18, 2012, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
christianu

Total Points: 481
Total Questions: 124
Total Answers: 99

Location: Trinidad and Tobago
Member since Thu, Dec 1, 2022
2 Years ago
christianu questions
;