The Wayback Machine - https://web.archive.org/web/20240602075753/https://www.geeksforgeeks.org/how-to-remove-the-table-row-in-a-table-using-javascript/
Open In App

How to remove the table row in a table using JavaScript ?

Last Updated : 14 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Removing a table row in JavaScript involves targeting the row element by its index or unique identifier, then using the remove() method to delete it from the DOM. This updates the table dynamically without requiring a page reload.

This can be done in two ways:

JavaScript remove() Method

This method removes the selected elements along with text and child nodes. This method also removes data and events of the selected elements. 

Syntax:

node.remove();

Example 1: This example first selects the row by id value and then removes it by using the remove() method. 

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>
        How to remove the table row 
        in a table using JavaScript ?
    </title>

    <style>
        table {
            margin: auto;
        }
        table, th, tr, td {
            border: 1px solid black;
        }
    </style>
</head>

<body style="text-align: center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h3>
        Remove Table Row from a Table
    </h3>

    <table>
        <tr>
            <th>S.No</th>
            <th>Title</th>
            <th>Geek_id</th>
        </tr>
        <tr id="row1">
            <td>Geek_1</td>
            <td>GeekForGeeks</td>
            <th>Geek_id_1</th>
        </tr>
        <tr>
            <td>Geek_2</td>
            <td>GeeksForGeeks</td>
            <th>Geek_id_2</th>
        </tr>
    </table>
    <br>

    <button onclick="Geeks()">
        Click Here
    </button>

    <script>
        function Geeks() {
            document.getElementById("row1").remove();
        }
    </script>
</body>

</html>

Output:

Removing table row using remove() method

Example 2: This example first selects the row by using the tag name and then removes the appropriate element by index using the remove() method. 

html
<!DOCTYPE html>
<html lang="en">

<head>
    <title>
        How to remove the table row 
        in a table using JavaScript ?
    </title>

    <style>
        table {
            margin: auto;
        }
        table, th, tr, td {
            border: 1px solid black;
        }
    </style>
</head>

<body style="text-align: center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h3>
        Remove Table row from a Table
    </h3>
    
    <table>
        <tr>
            <th>S.No</th>
            <th>Title</th>
            <th>Geek_id</th>
        </tr>
        <tr id="row1">
            <td>Geek_1</td>
            <td>GeekForGeeks</td>
            <th>Geek_id_1</th>
        </tr>
        <tr>
            <td>Geek_2</td>
            <td>GeeksForGeeks</td>
            <th>Geek_id_2</th>
        </tr>
    </table>
    <br>

    <button onclick="Geeks()">
        Click Here
    </button>

    <script>
        function Geeks() {
            document.getElementsByTagName("tr")[2].remove();
        }
    </script>
</body>

</html>

Output:

Using DOM deleteRow() method

This method is used for removing an <tr> element from a table.

Syntax:

tableObject.deleteRow(index);

Example: In this example, we will see the use of deleteRow() method.

Javascript
<!DOCTYPE html>
<html lang="en">

<head>
    <title>
        How to remove the table row 
        in a table using JavaScript ?
    </title>

    <style>
        table {
            margin: auto;
        }
        table, th, tr, td {
            border: 1px solid black;
        }
    </style>
</head>

<body style="text-align: center;">
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h3>
        Remove Table row from a Table
    </h3>
    
    <table id="newtable">
        <tr>
            <th>S.No</th>
            <th>Title</th>
            <th>Geek_id</th>
        </tr>
        <tr>
            <td>Geek_1</td>
            <td>GeekForGeeks</td>
            <th>Geek_id_1</th>
        </tr>
        <tr>
            <td>Geek_2</td>
            <td>GeeksForGeeks</td>
            <th>Geek_id_2</th>
        </tr>
    </table>
    <br>

    <button onclick="document.getElementById(
            'newtable').deleteRow(1)">
        Click Here
    </button>
</body>

</html>

Output:



Similar Reads

How to remove table row from table using jQuery ?
The jQuery remove() method is used to remove a row from HTML table. jQuery remove() Method: This method removes the selected elements alongwith text and child nodes. This method also removes data and events of the selected elements. Syntax: $(selector).remove(selector) Parameters: It accepts single parameter selector which is optional. It specifies
2 min read
How to add table row in a table using jQuery?
jQuery can be used to add table rows dynamically. This can be used in web applications where the user may need to add more rows if required. Steps to add table row: The required markup for the row is constructed. markup = &quot;&lt;tr&gt;&lt;td&gt; + information + &lt;/td&gt;&lt;/tr&gt;&quot; The table body is selected to which the table rows to be
2 min read
How to Insert a Row in an HTML Table in JavaScript ?
In JavaScript, we can insert a row dynamically in the HTML table using various approaches. JavaScript provides us with different methods to accomplish this task as listed below. Table of Content Using insertRow() and insertCell()Using insertAdjacentHTML()Using insertRow() and insertCell()In this approach, we are using the insertRow() and insertCell
2 min read
How to Remove Column from HTML Table using JavaScript ?
Given an HTML table and the task is to remove the certain column from the HTML table. There are two approaches that are discussed below: Approach 1: First, select the table and also get the rows of table using table.rows. Get the number of columns of a row and go through each one of the columns. Use str.search("someColumnName") (Because, we want to
3 min read
How to select the last row of a table using jQuery ?
Given an HTML table and the task is to select the last row of the table with the help of jQuery. There are two approaches that are discussed below with the proper example. Approach 1: First, select the table by its ID. Use find() method to find the all table rows of the table. Use last() method to get the last row of table. The background color of
2 min read
How to create a table row using HTML5 ?
In this article, we define a row in a table by using a &lt;tr&gt; tag in a document. This tag is used to define a row in an HTML table. The tr element contains multiple th or td elements. Syntax: &lt;tr&gt; ... &lt;/tr&gt; Example: &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt; How to define row of a table &lt;/title&gt; &lt;style
1 min read
How to show contents of selected row of a table in Bootstrap model using jQuery ?
The task is to fetch data from a row of the table and show them on the bootstrap model. Bootstrap model: The Modal component is a kind of dialog box or a popup window that is displayed on top of the present page, on clicking the provided button on the page. However, the Model automatically closes on clicking the model's backdrop. Moreover, it shoul
4 min read
How to Delete a Row from Table using AngularJS ?
Given a HTML table and the task is to remove/delete the row from the table with the help of AngularJS. Approach: The approach is to delete the row from the array where it stored and served to the table data. When the user clicks on the button near to the table row, it passes the index of that table and that index is used to remove the entry from th
2 min read
How to highlight alternate table row using jQuery ?
In this article, we will set the highlight on an alternative table row using jQuery. The :nth-child(2n) selector is used to select the alternative row and the addClass() method is used to set the style of the alternative rows. Syntax: $(tr:nth-child(2n)").addClass("GFG"); Here, we will create a simple table using the &lt;table&gt; tag. The &lt;thea
2 min read
How to set alternate table row color using CSS?
To set alternate table row colors using CSS, utilize the :nth-child() pseudo-class to target even and odd rows. Apply background-color styles to each row accordingly, ensuring readability and enhancing the visual presentation of tabular data. Using the :nth-child() pseudo-classThe :nth-child() selector in CSS is used to match the elements based on
2 min read