Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to hide the table header using JavaScript ?

  • Last Updated : 30 Jun, 2020

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

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
Recommended Articles
Page :

Start Your Coding Journey Now!