Open In App

How to hide the table header using JavaScript ?

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see the methods to hide the table header using JavaScript. There are two approaches that can help to hide a table header with the help of JavaScript. 

They are discussed below:

Approach 1: Select the header using a CSS selector and modify the style property such that the value of the display property is set to none. This will hide the selected table header element from the page.

Example: This example shows the use of the above-explained approach.

html




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        How to hide the table header
        using JavaScript ?
    </title>
 
    <style>
        table, tr th, td {
            border: 1px solid black;
        }
 
        th, td {
            padding: 10px;
        }
 
        table {
            margin: auto;
        }
    </style>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
 
    <table id="table">
        <tr id="thead">
            <th>S.No</th>
            <th>Title</th>
        </tr>
        <tr>
            <td>Geek_1</td>
            <td>GeekForGeeks</td>
        </tr>
        <tr>
            <td>Geek_2</td>
            <td>GeeksForGeeks</td>
        </tr>
        <tr>
            <td>Geek_3</td>
            <td>GeeksForGeeks</td>
        </tr>
    </table>
    <br>
 
    <button onclick="Geeks()">
        Click Here
    </button>
     
    <p id="GFG"></p>
 
    <script>
        var elm = document.getElementById("GFG");
 
        function Geeks() {
            let tableHead = document.getElementById("thead");
            tableHead.style.display = "none";
            elm.innerHTML = "Table header is hidden";
        }
    </script>
</body>
 
</html>


Output: 

 

Approach 2: Use jQuery to select the header to be hidden and use the hide() method on it. This jQuery method helps to hide the element on which it is used.

Example: This example shows the use of the above-explained approach.

HTML




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>
        How to hide the table header
        using JavaScript ?
    </title>
 
    <style>
        table,
        tr th,
        td {
            border: 1px solid black;
        }
 
        th,
        td {
            padding: 10px;
        }
 
        table {
            margin: auto;
        }
    </style>
 
    <script src=
    </script>
</head>
 
<body style="text-align:center;">
 
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
 
    <table id="table">
        <tr id="thead">
            <th>S.No</th>
            <th>Title</th>
        </tr>
        <tr>
            <td>Geek_1</td>
            <td>GeekForGeeks</td>
        </tr>
        <tr>
            <td>Geek_2</td>
            <td>GeeksForGeeks</td>
        </tr>
        <tr>
            <td>Geek_3</td>
            <td>GeeksForGeeks</td>
        </tr>
    </table>
    <br>
 
    <button onclick="Geeks()">
        Click Here
    </button>
 
    <p id="GFG"></p>
 
    <script>
        var elm = document.getElementById("GFG");
 
        function Geeks() {
            $("#thead").hide();
            elm.innerHTML = "Table header is hidden";
        }
    </script>
</body>
 
</html>


Output:

 



Last Updated : 28 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads