Skip to content
Related Articles

Related Articles

How to hide the table header using JavaScript ?

Improve Article
Save Article
  • Last Updated : 30 Jun, 2020
Improve Article
Save Article

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:

html




<!DOCTYPE HTML>
<html>
<head>
    <title>
      How to hide the table
      header using JavaScript
    </title>
    <style>
        table {
            margin-left: 180px;
        }
  
        td {
            padding: 10px;
        }
    </style>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <table id="table">
        <colgroup>
            <col id="myCol"
                 span="2">
            <col>
        </colgroup>
        <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_DOWN">
    </p>
  
    <script>
        var el_down =
            document.getElementById("GFG_DOWN");
        function Geeks() {
            var tb
            = document.getElementById("thead");
            tb.style.display = "none";
            el_down.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:

html




<!DOCTYPE HTML>
<html>
<head>
    <title>
      How to hide the table 
      header using JavaScript
    </title>
    <script src =
    </script>
    <style>
        table {
            margin-left: 180px;
        }
  
        td {
            padding: 10px;
        }
    </style>
</head>
<body style="text-align:center;">
    <h1 style="color:green;">
        GeeksForGeeks
    </h1>
    <table id="table">
        <colgroup>
            <col id="myCol" 
                 span="2">
            <col>
        </colgroup>
        <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_DOWN">
    </p>
  
    <script>
        var el_down = 
            document.getElementById("GFG_DOWN");
        function Geeks() {
            $("#thead").hide();
            el_down.innerHTML =
              "Table header is hidden";
        
    </script>
</body>
</html>

Output:


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!