
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
How to hide HTML element with JavaScript?
In this tutorial, we will learn how to hide HTML element with JavaScript.
Hiding an HTML element can be performed in different ways in JavaScript. In this tutorial, we will see the three most popular ways of doing it −
- Using the hidden property
- Using the style.display property
- Using the style.visibility property
Generally, we use the hidden attribute to hide a particular element. We can toggle between hiding and showing the element by setting the hidden attribute value to true or false, respectively.
In the other two ways, we use the style object of the element. We have two properties in the style object to hide the HTML element, one is the display, and another one is the visibility.
In JavaScript, we can use both of these properties to hide the HTML elements, but the main difference between these two is when we use style.visibility property, then the specific tag is not visible, but the space of the tag is still allocated. Whereas in style.display property, not only is the tag hidden but also there is no space allocated to that element.
Using the hidden property
In JavaScript, the hidden property of an element is used to hide an element. We set the hidden properties value to true to hide the element.
Syntax
Following is the syntax to use hidden property −
document.getElementById('element').hidden = true
In the above syntax, ‘element’ is the id of an HTML element, and by using document.getElementById method, we are accessing the element and changing its hidden property to true to hide the element.
Example
In the below example, we have used the hidden property to hide a div element using JavaScript.
<html> <body> <div id="dip"> Click the below buttons to hide or show this text. </div><br> <button onclick="hide()"> Hide Element </button> <button onclick="show()"> Show Element </button> <script> function hide() { document.getElementById('dip').hidden = true } function show() { document.getElementById('dip').hidden = false } </script> </body> </html>
Using the style.display property
In JavaScript, style.display property is also used to hide the HTML element. It can have values like ‘block,’ ‘inline,’ ‘inline-block,’ etc., but the value used to hide an element is ‘none.’ Using JavaScript, we set the style.display property value to ‘none’ to hide html element.
Syntax
Following is the syntax to hide HTML elements using style.display property in JavaScript.
document.getElementById('element').style.display = 'none'
In the above syntax, ‘element’ is the id of an HTML element, and by using document.getElementById method, we are accessing the element and changing its style.display property to ‘none’ to hide the element.
Example
In the below example, we have used the style.display property to hide a div element using JavaScript.
<html> <style> #myDIV { width: 630px; height: 300px; background-color: #F3F3F3; } </style> <body> <p> Click the "Hide Element" button to hide the div element. </p> <button onclick="hide()"> Hide Element </button> <div id="myDIV"> Hello World! This is DIV element </div> <script> function hide() { document.getElementById('myDIV').style.display = 'none' } </script> </body> </html>
Using the style.visibility property
In JavaScript, style.visibility property is also used to hide the HTML element. It can have values like ‘visible,’ ‘collapse,’ ‘hidden’, ‘initial’ etc., but the value used to hide an element is ‘hidden’. Using JavaScript, we set the style.visibility property value to ‘hidden’ to hide html element.
Syntax
Following is the syntax to hide HTML elements using style.visibility property in JavaScript −
document.getElementById('element').style.visibility = 'hidden'
In the above syntax, ‘element’ is the id of an HTML element, and by using document.getElementById method, we are accessing the element and changing its style.visibility property to ‘hidden’ to hide the element.
Example
In the below example, we have used the style.visibility property to hide element using JavaScript.
<html> <style> #dip { width: 630px; height: 300px; background-color: #F3F3F3; } </style> <body> <p> Click the "Hide Element" button to hide the div element. </p> <button onclick="hide()"> Hide Element </button> <button onclick="show()"> Show Element </button> <div id="dip"> Hello World! This is DIV element </div> <script> function hide() { document.getElementById('dip').style.visibility = 'hidden'; } function show() { document.getElementById('dip').style.visibility = 'visible'; } </script> </body> </html>
In the above output, users can see the element is hidden using style.visibility property, but the element still occupies its space in the browser.
In this tutorial, we learned three ways to hide an element using JavaScript. The first approach was to use the hidden property of an element. The next was to set style.display property to ‘hidden’. The third method was to set style.visibility property to ‘hidden’.
- Related Articles
- How to display HTML element with JavaScript?
- How to hide/show HTML elements in JavaScript?
- How can I show and hide an HTML element using jQuery?
- Add oninput attribute to HTML element with JavaScript?
- How to create a draggable HTML element with JavaScript and CSS?
- How to replace an HTML element with another one using JavaScript?
- Hide an element from view with CSS
- How to add many Event Handlers to the same element with JavaScript HTML DOM?
- Replace HTML div into text element with JavaScript?
- How to hide scrollbars with CSS?
- How to add a new element to HTML DOM in JavaScript?
- How to hide a navigation menu on scroll down with CSS and JavaScript?
- How to add an element horizontally in HTML page using JavaScript?
- How to remove “disabled” attribute from HTML input element using JavaScript?
- Apply an IF condition to every element in an HTML table with JavaScript?
