Featured Article

W3Schools JavaScript Hide and Show: Easy Toggle Techniques for Beginners

Adam Jul 13, 2026

JavaScript, a versatile programming language, offers numerous ways to manipulate the Document Object Model (DOM) and control the visibility of elements. One of the fundamental tasks is to hide or show elements based on certain conditions or user interactions. This article explores theοΏ½es' approach to hide and show elements in JavaScript, with references to W3Schools, a popular resource for web developers.

Fun JavaScript Coding Project Ideas for Beginners: Practice Front End Web Development (in 2020)
Fun JavaScript Coding Project Ideas for Beginners: Practice Front End Web Development (in 2020)

JavaScript provides several methods to manipulate the CSS styles of DOM elements, thereby controlling their visibility. We will delve into two primary methods: using `style.display` property and the `className` property along with CSS classes.

JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)
JavaScript Cheatsheet for Beginners | Learn JS Fast (One-Page Guide)

Hiding and Showing Elements with style.display

The `style.display` property allows us to set the CSS display style of an element, which can be used to hide or show elements. The possible values for `display` include `block`, `none`, `inline`, and `inline-block`.

Create Image Accordion Slider With HTML CSS And JavaScript
Create Image Accordion Slider With HTML CSS And JavaScript

To hide an element, set its `display` property to `none`; to show an element, set its `display` property to `block` or `inline` (depending on the element's naturally occurring display style).

Hiding an Element

JavaScript Full Notes Every Beginner Must Save πŸ’‘
JavaScript Full Notes Every Beginner Must Save πŸ’‘

To hide an element, use the following syntax:

document.getElementById('elementId').style.display = 'none';

This code retrieves the element with the ID 'elementId' and sets its `display` style to `none`, making it invisible on the page.

Best JavaScript Projects for Beginners to Practice
Best JavaScript Projects for Beginners to Practice

Showing an Element

To show a hidden element, use the following syntax:

document.getElementById('elementId').style.display = 'block';

HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)
HTML vs CSS vs JavaScript: What Each One Actually Does (Beginner's Visual Guide)

This code retrieves the element with the ID 'elementId' and sets its `display` style to `block`, making it visible again.

Hiding and Showing Elements with className and CSS Classes

JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“
JavaScript Template Literals: Write Cleaner & Dynamic Strings πŸ“
JavaScript Tips to Change Border Radius & Background Color
JavaScript Tips to Change Border Radius & Background Color
Build a Weather App with JavaScript  | Beginner-Friendly Project
Build a Weather App with JavaScript | Beginner-Friendly Project
Dynamic Class Toggle in DOM
Dynamic Class Toggle in DOM
Javascript String Methods
Javascript String Methods
How to Create a Function in JavaScript: Step-by-Step Guide πŸš€
How to Create a Function in JavaScript: Step-by-Step Guide πŸš€
Javascript Project Ideas πŸ‘
Javascript Project Ideas πŸ‘
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
JavaScript Cheat Sheet You Must Save | Beginner Friendly Guide
a poster with different types of projects and instructions for students to use in the classroom
a poster with different types of projects and instructions for students to use in the classroom
JavaScript Works in the Browser
JavaScript Works in the Browser
an image of programming tricks and tips for beginners to learn in this video game
an image of programming tricks and tips for beginners to learn in this video game
JavaScript Methods: Add Functionality to Your Objects πŸš€
JavaScript Methods: Add Functionality to Your Objects πŸš€
Filter JavaScript Arrays Like a BOSS: Beginner's Guide
Filter JavaScript Arrays Like a BOSS: Beginner's Guide
CodeGuppy: The JavaScript Platform for Kids
CodeGuppy: The JavaScript Platform for Kids
the web programming poster is shown
the web programming poster is shown
JavaScript Project Under 30 Lines of Code | Easy Beginner DOM Project Tutorial
JavaScript Project Under 30 Lines of Code | Easy Beginner DOM Project Tutorial
Functions in Javascript
Functions in Javascript
a woman with blonde hair standing in front of a sign that says learn java script by building 7 games
a woman with blonde hair standing in front of a sign that says learn java script by building 7 games
JavaScript loops by Duomly
JavaScript loops by Duomly
JavaScript Fundamental
JavaScript Fundamental

Another approach to hide and show elements involves manipulating the `className` property of elements in conjunction with CSS classes. This method is more flexible and can be used to apply multiple styles to an element.

Create a CSS class with `display: none;` to hide elements and another class with `display: block;` to show elements. Toggle the `className` property of the element to swap between these two classes.

Hiding an Element

Create a CSS class for hiding elements:

.hide { display: none; }

Then, toggle the `className` property of the element:

document.getElementById('elementId').className = 'hide';

This code adds the `hide` class to the element, making it invisible.

Showing an Element

Create a CSS class for showing elements:

.show { display: block; }

Then, toggle the `className` property of the element:

document.getElementById('elementId').className = 'show';

This code replaces the `hide` class with the `show` class, making the element visible again.

Mastering these techniques will enable you to create dynamic and interactive web applications with JavaScript. Happy coding, and don't forget to explore W3Schools for more in-depth learning and practice exercises!