Friday, January 16, 2026
HomeLanguagesJavascriptHow to Hide an HTML Element by Class using JavaScript ?

How to Hide an HTML Element by Class using JavaScript ?

Suppose you have given an HTML document and the task is to hide an HTML element by its class name with the help of JavaScript. There are two approaches to explain with the proper example.

Approach 1: In this approach, getElementsByClassName() selector is used to select elements of specific class. Indexing is used to get the element at respective index. To get the access to the CSS visibility property, We can use DOM style.visibility on the elements to set it to hidden value.

  • Example:




    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            How to hide an HTML element
            by class in JavaScript
        </title>
        <script src=
        </script>
        <style>
            body {
                text-align: center;
            }
              
            h1 {
                color: green;
            }
              
            .neveropen {
                color: green;
                font-size: 24px;
                font-weight: bold;
            }
        </style>
    </head>
      
    <body>
        <h1> 
            neveropen 
        </h1>
        <b> 
            Click on button to hide the element 
            by class name
        </b>
        <br>
        <div class="outer">
            <div class="child1">Child 1</div>
            <div class="child1">Child 1</div>
            <div class="child2">Child 2</div>
            <div class="child2">Child 2</div>
        </div>
        <br>
        <button onClick="GFG_Fun()">
            click here
        </button>
        <p id="neveropen">
        </p>
        <script>
            var down = document.getElementById('GFG_DOWN');
      
            function GFG_Fun() {
                document.getElementsByClassName('child1')[0].
                style.visibility = 'hidden';
                down.innerHTML = "Element is hidden";
            }
        </script>
    </body>
      
    </html>

    
    
  • Output:

Approach 2: In this approach, querySelectorAll() selector is used to select elements of specific class. Indexing is used to get the element at respective index. To get the access to the CSS visibility property, We can use DOM style.visibility on the elements to set it to hidden value.

  • Example:




    <!DOCTYPE HTML>
    <html>
      
    <head>
        <title>
            How to hide an HTML element
            by class in JavaScript
        </title>
        <script src=
        </script>
        <style>
            body {
                text-align: center;
            }
              
            h1 {
                color: green;
            }
              
            .neveropen {
                color: green;
                font-size: 24px;
                font-weight: bold;
            }
        </style>
    </head>
      
    <body>
        <h1> 
            neveropen 
        </h1>
        <b> 
            Click on button to hide the element 
            by class name
        </b>
        <br>
        <div class="outer">
            <div class="child1">Child 1</div>
            <div class="child1">Child 1</div>
            <div class="child2">Child 2</div>
            <div class="child2">Child 2</div>
        </div>
        <br>
        <button onClick="GFG_Fun()">
            click here
        </button>
        <p id="neveropen"></p>
      
        <script>
            var down = document.getElementById('GFG_DOWN');
      
            function GFG_Fun() {
                document.querySelectorAll('.child1')[0].
                style.visibility = 'hidden';
                down.innerHTML = "Element is hidden";
            }
        </script>
    </body>
      
    </html>

    
    
  • Output:
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32474 POSTS0 COMMENTS
Milvus
117 POSTS0 COMMENTS
Nango Kala
6846 POSTS0 COMMENTS
Nicole Veronica
11977 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12062 POSTS0 COMMENTS
Shaida Kate Naidoo
6985 POSTS0 COMMENTS
Ted Musemwa
7219 POSTS0 COMMENTS
Thapelo Manthata
6933 POSTS0 COMMENTS
Umr Jansen
6911 POSTS0 COMMENTS