Hide or show HTML elements using visibility property in JavaScript
The visibility property is used to hide or show the content of HTML elements. The visibility property specifies that the element is currently visible on the page. The ‘hidden’ value can be used to hide the element. This hides the element but does not remove the space taken by the element, unlike the display property.
Syntax:
element.style.visibility = 'hidden'; element.style.visibility = 'visible';
Example:
<!DOCTYPE html> <html> <head> <title> Hide or show HTML elements using visibility property in JavaScript </title> <style> .container { height: 80px; width: 250px; border: 2px solid black; background-color: green; color: white; </style> </head> <body> <center> <div class="container"> <h1>GeeksforGeeks</h1> </div> <p> Click the buttons to show or hide the green box </p> <button onclick="showElement()"> Show Element </button> <button onclick="hideElement()"> Hide Element </button> </center> <script type="text/javascript"> function showElement() { element = document.querySelector('.container'); element.style.visibility = 'visible'; } function hideElement() { element = document.querySelector('.container'); element.style.visibility = 'hidden'; } </script> </body> </html> |
Output:
- Clicking the Hide button:
- Clicking the Show button:
Note: Here after clicking the hide button the element will become invisible and left the space blank.
Recommended Posts:
- Hide or show elements in HTML using display property
- Show and hide password using JavaScript
- How to Show and Hide div elements using Checkboxes ?
- How to Show and Hide div elements using radio buttons?
- How to display icon next to show/hide text on button?
- How to hide or show one record from an ng-repeat within a table based on ng-click?
- jQuery | Hide/Show, Toggle and Fading methods with Examples
- HTML | DOM Style visibility Property
- CSS | visibility Property
- CSS | backface-visibility Property
- Hide the cursor in a webpage using CSS and JavaScript
- How to hide Bootstrap modal with JavaScript?
- Visibility of elements in bootstrap with Examples
- How to hide elements in responsive layout using CSS ?
- p5.js | show() Function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



