How to Hide an HTML Element by Class using JavaScript? Last Updated : 15 Oct, 2024 Comments Improve Suggest changes Like Article Like Report To hide an HTML element by class using JavaScript, the CSS display property can be manipulated.Below are the approaches to hide an HTML element by class:Table of ContentUsing getElementsByClassName() selectorUsing querySelectorAll() selectorApproach 1: Using getElementsByClassName() selectorIn this approach, getElementsByClassName() selector is used to select elements of a specific class. Indexing is used to get the element at the respective index. To get access to the CSS visibility property, We can use DOM style.visibility on the elements to set it to a hidden value.Example: This example shows the use of the above-explained approach. HTML <!DOCTYPE HTML> <html> <head> <title> How to hide an HTML element by class in JavaScript </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body> <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="geeks"> </p> <script> let 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: Using querySelectorAll() selectorIn 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: This example shows the implementation of the above-mentioned approach.HTML<!DOCTYPE HTML> <html> <head> <title> How to hide an HTML element by class in JavaScript </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body> <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="geeks"></p> <script> let down = document.getElementById('GFG_DOWN'); function GFG_Fun() { document.querySelectorAll('.child1')[0]. style.visibility = 'hidden'; down.innerHTML = "Element is hidden"; } </script> </body> </html> Output: Approach 2: Using querySelectorAll() selectorIn 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: This example shows the implementation of the above-mentioned approach. HTML <!DOCTYPE HTML> <html> <head> <title> How to hide an HTML element by class in JavaScript </title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> </head> <body> <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="geeks"></p> <script> let down = document.getElementById('GFG_DOWN'); function GFG_Fun() { document.querySelectorAll('.child1')[0]. style.visibility = 'hidden'; down.innerHTML = "Element is hidden"; } </script> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Hide an HTML Element by Class using JavaScript? P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Questions Similar Reads How to Toggle an Element Class in JavaScript ? In JavaScript, toggling an element class refers to the process of dynamically adding or removing a CSS class from an HTML element. This allows developers to easily change an element's appearance or behavior in response to user actions, such as clicks or events.These are the following methods for tog 2 min read How to Filter a DIV Element Based on its Class Name using JavaScript? Div Element can be filtered based on class name for displaying specific content using JavaScript. Here, we will explore two different approaches to filtering a DIV element.Table of Content Using querySelectorAll and classListUsing getElementsByClassNameUsing querySelectorAll and classListIn this app 3 min read How To Get Element By Class Name In JavaScript ? When working with the DOM in JavaScript, selecting elements by their class names is a common task. JavaScript provides several methods to achieve this, whether we need to select one or multiple elements. In this article, we will cover different approaches to get elements by class name in JavaScript. 3 min read How to remove a Class name from an Element using JavaScript ? Removing a class name from an HTML element using JavaScript is often used to dynamically modify the styling or behavior of an element based on user interactions, events, or other conditions. The operation can be achieved using classList property, that allows you to add, remove, or toggle classes. Sy 3 min read How to Add a Class to DOM Element in JavaScript? Adding a class to a DOM (Document Object Model) element in JavaScript is a fundamental task that enables developers to dynamically manipulate the appearance and behavior of web pages. Classes in HTML provide a powerful way to apply CSS styles or JavaScript functionality to multiple elements at once. 2 min read How to Get Value by Class Name using JavaScript ? This article will show you how to use JavaScript to get value by class name. To get the value of an element by its class name in JavaScript, you can use the getElementsByClassName() method. This method returns an array-like object of all elements with the specified class name. You can then access th 2 min read How to Hide an HTML Element in Mobile View using jQuery ? Suppose we have given an HTML document and the task is to hide an HTML element in mobile view with the help of jQuery. Approach: Initially, we are required to detect whether our system is 'Mobile' or not, for that window.navigator.userAgent property is used. It returns a string containing informatio 2 min read How to hide div element by default and show it on click using JavaScript and Bootstrap ? We have to first hide the div and when a user will click the button we need to display that hiden div on the screen. It is helpul for creating any type of form. There are two methods to solve this problem which are discussed below:Â Table of Content Using .show() methodUsing .toggle() methodApproach 2 min read How to get/change the HTML with DOM element in JavaScript ? In order to get/access the HTML for a DOM element in JavaScript, the first step is to identify the element base on its id, name, or its tag name. Then, we can use inner.HTML or outer.HTML to get the HTML. Using the getElementById() method: This method gets/identifies the DOM elements using its ID an 3 min read How to Find Out if an Element is Hidden with JavaScript? JavaScript provides the feature of DOM manipulation, allowing developers to dynamically interact with and modify the content and structure of a web page. One common task is determining if an element is hidden, which can be achieved through various methods such as checking the element's display or vi 3 min read Like