How 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:

html

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>

chevron_right


Output:

full-stack-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.