All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Hide Element By Class

Last Updated : Mar 11, 2024

JavaScript Hide Element By Class

In this tutorial we will show you the solution of JavaScript hide element by class, here we are going to show you example by using display and visibility property.

The display property is used to hide and show the contents of HTML DOM using javascript.

If we want to hide the element set the style display property to ‘none’ otherwise we can set it as ‘block’ which default property value also for elements.

The visibility property also same as above said we can use this for both hide and show html elements.

Step By Step Guide On JavaScript Hide Element By Class :-

Here we defined h1 tag for display heading, div element with some codes of styles and two buttons defined ‘None, Hide’ with id’s ‘dis_n,vis_h’ for trigger click event.

In script when user clicks on ‘None, Hide’ any buttons which triggers click event by ‘addEventListener’ then it loads function.

There we collecting div element class ‘b’ by getElementsByClassName() method then we sets style properties of display or visibility with respective values ‘None, Hidden’ and it will hides element.

<!DOCTYPE html>
<html>
    <head>
        <title>HIDE BY CLASS</title>
    </head>
    <body>
        <style>
            #b{
                padding: 2rem;
                border: 5px solid rgb(18, 220, 89);
                width: max-content;
            }
        </style>
        <h1>Hide Elements By Class Name</h1>
        <div id="b" class="b">You can hide/show this box using javascript</div>
        <br>
        <button id="dis_n">None</button>
        <button id="vis_h">Hide</button>
        <script>
            document.getElementById("dis_n").addEventListener("click",function(){
                document.getElementsByClassName("b")[0].style.display="none";
            });
            document.getElementById("vis_h").addEventListener("click",function(){
                document.getElementsByClassName("b")[0].style.visibility="hidden";
            });
        </script>
    </body>
</html>
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contain information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined h1 tag with ‘Hide Elements By Class Name’ heading, div element with some lines of style by using class name ‘b’ then we defined ‘None, Hide’ buttons with separate id’s ‘dis_n,vis_h’ for access those elements.
  7. In script we referred each buttons by specifing their id’s in getElementById() method then which is appends with ‘addEventListener()’ for listen when click event occur.
  8. Within that we collecting div element by class name ‘b’ then we sets display property to ‘none’ for hide state and likewise we sets ‘b’ visibility property to ‘hidden’ for hide state.
  9. Here must we need to do another thing is nearby getElementsByClassName() method we have specify [0] for work this concept correctly otherwise we can’t get the result because it is defined for handle array.
  10. If you handle this as like getElementById() method which can’t returns anything because getElementById() method defined with other behavior so we can easily access it.
  11. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know how to define hide element by class name in javascript.

When we executes program on browser we can see heading, div section with some styles and two buttons None, Hide if user clicks on ‘None’ then div will disappeared like ‘Hide’ also do the same.

If user things to hide other element then define them then use this concept get successful result.

I hope this tutorial on JavaScript hide element by class helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Amruta

Amruta is an Experienced web developer with 4 years for experience she completed her master's with MCA and passionate about programming Languages for creating technical contents like HTML, CSS, JavaScript, Java, Python, PHP, jQuery.

Follow Amruta On Linkedin 🡪