How to Delete a Row in JavaScript?

Estimated read time 1 min read

To delete a row in JavaScript, you first need to select the HTML table that contains the row you want to delete. You can do this using the document.getElementById() or document.querySelector() methods to get a reference to the table element. Once you have a reference to the table, you can use the deleteRow() method to remove the row from the table.

Here’s an example of how to delete a row from a table with an ID of my-table:

// Get a reference to the table
var table = document.getElementById("my-table");

// Get a reference to the row you want to delete
var row = table.rows[rowIndex];

// Delete the row
table.deleteRow(rowIndex);

In this example, rowIndex is the index of the row you want to delete (starting at 0 for the first row). The rows property of the table element is an array-like object that contains all the rows in the table. You can access a specific row using its index.

Note that when you delete a row from a table, all the cells in that row will also be removed. If you need to delete only a specific cell, you can use the deleteCell() method instead.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply