Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Filter a DIV Element Based on its Class Name using JavaScript?
Next article icon

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 Content

  • Using getElementsByClassName() selector
  • Using querySelectorAll() selector

Approach 1: Using getElementsByClassName() selector

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

output

Approach 2: Using querySelectorAll() selector

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

output


Next Article
How to Filter a DIV Element Based on its Class Name using JavaScript?

P

PranchalKatiyar
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-Questions

Similar Reads

  • How to remove an HTML element using JavaScript ?
    Removing an HTML element using JavaScript involves deleting it from the DOM, typically using methods like element.remove() or parentNode.removeChild(). This approach updates the webpage dynamically, allowing for responsive interactions by removing unwanted or outdated elements based on user actions
    3 min read
  • 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 to
    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 ap
    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 create Popup Box using HTML CSS and JavaScript?
    Creating a popup box with HTML, CSS, and JavaScript improves user interaction on a website. A responsive popup appears when a button is clicked, featuring an HTML structure, CSS for styling, and JavaScript functions to manage visibility. ApproachCreate the Popup structure using HTML tags, Some tags
    3 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 create a Popup Form using HTML CSS and JavaScript ?
    To create a popup form using JavaScript, you can design a hidden overlay that becomes visible when triggered. We will use HTML for form elements, CSS for styling, and JavaScript to toggle the visibility of the overlay. This allows for a user-friendly and interactive way to display forms on your webp
    3 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences