Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 10, 2023 03:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/9ab78307044703c3120419bd802ff4fb to your computer and use it in GitHub Desktop.
Save code-boxx/9ab78307044703c3120419bd802ff4fb to your computer and use it in GitHub Desktop.
Javascript Remove HTML Table Rows & Cells

JAVASCRIPT REMOVE TABLE ROWS & CELLS

https://code-boxx.com/remove-table-rows-cells-javascript/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>Get Row Cell</title>
</head>
<body>
<!-- (A) DUMMY TABLE -->
<table id="demoA">
<tr><td>First</td></tr>
<tr><td>Second</td></tr>
<tr><td>Third</td></tr>
<tr><td>Forth</td></tr>
</table>
<!-- (B) JAVSCRIPT GET ROWS & CELLS -->
<script>
function getRowCell () {
// (B1) GET TABLE
var table = document.getElementById("demoA");
// (B2) LOOP THROUGH ROWS & CELLS
// TABLE.ROWS & ROW.CELLS ACT JUST LIKE ARRAYS
for (let row of table.rows) {
console.log(row);
for (let cell of row.cells) {
console.log(cell);
}
}
// (B3) ACCESS INDIVIDUAL ROW & CELL
var allRows = table.rows.length; // total number of rows
var firstRow = table.rows[0];
var lastRow = table.rows[allRows-1];
var firstRowCell = firstRow.cells[0]; // first cell of first row
}
</script>
<input type="button" value="Get Row Cell" onclick="getRowCell()"/>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Remove Row Cell</title>
</head>
<body>
<!-- (A) DUMMY TABLE -->
<table id="demoB">
<tr> <td>A</td> <td>B</td> </tr>
<tr> <td>C</td> <td>D</td> </tr>
<tr> <td>D</td> <td>E</td> </tr>
</table>
<!-- (B) JAVSCRIPT REMOVE ROWS & CELLS -->
<script>
function delRowCell () {
// (B1) GET TABLE
var table = document.getElementById("demoB");
// (B2) REMOVE SECOND ROW
table.deleteRow(1);
// (B3) REMOVE FIRST CELL OF FIRST ROW
var firstRow = table.rows[0];
firstRow.deleteCell(0);
}
</script>
<input type="button" value="Delete Row Cell" onclick="delRowCell()"/>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Remove Row Cell</title>
</head>
<body>
<!-- (A) DUMMY TABLE -->
<table id="demoC">
<tr> <td class="cellC">A</td> <td>B</td> </tr>
<tr id="rowC"> <td>C</td> <td>D</td> </tr>
<tr> <td>D</td> <td class="cellC">E</td> </tr>
</table>
<!-- (B) JAVSCRIPT REMOVE ROWS & CELLS -->
<script>
function delRowCell () {
// (B1) REMOVE ROW BY ID
document.getElementById("rowC").remove();
// (B2) REMOVE CELLS BY CSS CLASS
for (let c of document.querySelectorAll("#demoC td.cellC")) {
c.remove();
}
}
</script>
<input type="button" value="Delete Row Cell" onclick="delRowCell()"/>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment