0

So basically my php file will return an echo (as a table), and in that table, each row will have a cell that is a delete button to delete that row. whatI want is when button clicked, the row will be delete from table. So I guess the row index is important to implement this. But how can I get row index in this situation in javascript?

2
  • You don't need to get the rowIndex. Any node can be deleted by node.parentNode.removeChild(node), including table rows. Note that the parent of a row is always a table section element (tbody, thead, tfoot), not the table element. Commented Oct 16, 2017 at 6:06
  • Probably a duplicate of Add/Delete table rows dynamically using JavaScript. Commented Oct 16, 2017 at 6:10

1 Answer 1

3

DOM table element has the method deleteRow and you can get the row index of a table row using rowIndex attribute.

var myTable = ...
var rowIndex = btn.parentNode.parentNode.rowIndex;
myTable.deleteRow(rowIndex);`
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.