EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

JavaScript Style visibility

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » JavaScript Tutorial » JavaScript Style visibility

JavaScript Style Visibility

Introduction to JavaScript Style visibility

JavaScript Style visibility property allows users to show or hide an element. This visibility property defines that a particular element is visible on the webpage. Same visibility there is one another property present in the JavaScript called hidden, it will also helps to hide the element but it will not remove the space which is already occupied by element. HTML elements use either inline or block display types. So elements from inline-block will be floats either left or right position where as block display will fill the entire line or block. So whenever visibility property set to hidden, in this case, elements will be displayed at its rightful place.

JavaScript Style visibility

  • As earlier we had discussed one can set visibility property to the element, to show or hide an element from the page. So by using visibility hidden value, it will be displayed at the rightful place which means the element will be invisible but it will hold its original position and size.
  • One can set visibility in styling by using values like visible, hidden, collapse, initial and inherit.
  • One can do both functions like hiding an element as well as removing an element from the document by setting the display property to none value rather than using visibility.
  • With the help of visibility: hidden we can save the space which is going to be occupied by elements on the screen but it will look simply blank. It’s made possible to do animation using visibility property which is not possible to do with display: none property.
  • JavaScript style display property is responsible to do setting and returning the value of the display type of selected element.
  • One more feature of visibility is an interpolation. This term defined as the visibility values taken as interpolable among the visibility values like visible and no visible so in this scenario start values or end values must be needed to be visible or there is no interpolation is going to happen.
  • Those visible values going to be used with timing function which is placed in between 0 and 1 map going to be user visible.
  • With the of visibility value of the hidden elements that will remove from the accessibility tree also which causes related elements as well as all descendant elements which is no longer going to be published by a technology called as screen reading.

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

There are few different values goes with visibility syntax let’s see one by one as follows:

object.style.visibility: This syntax will help us to set visibility property to the element so one can show or hide elements as per their choice.

  • element.style.visbility =”hidden”;
  • element.style.visibility=”visible”;

Visibility can be set with values visible, hidden, collapse, initial, inherit.

  • visible: The property value is set as visible to show contents on the screen. By default property value set to visible.
  • hidden: This property value is to hide the contents. Here the element is hidden but still occupies the space on the screen.
  • collapse: Collapse is the property value only going to be used with the table element. It’s mainly used to remove a particular row or column. So the table’s layout will remain as it is. By using collapse property in the table, the space occupied by row and column will get free for other elements in the layout.
  • initial: This property value is used to the default value.
  • inherit: It’s one of the property values used o inherit contents from its parent element.

Examples to Implement JavaScript Style visibility

Below are the examples of JavaScript Style visibility:

Example #1

To hide content by clicking on the link:

Code:

<!DOCTYPE html>
<html>
<head>
<style type="text/css">
#visibility_demo {
width:500px;
margin:20px auto;
text-align: center;
}
#block1{
height:50px; background: lightgreen;
color:#fff;
padding-top: 30px;
visibility: visible;
}
#block2 {
height:50px;
background:blue;
color:#fff;
padding-top: 30px;
}
#block3 {
height:50px;
background:black;
color:#fff;
padding-top: 30px;
}
</style>
</head>
<body>
<div id="visibility_demo">
<h4>Example of JavaScript Style Visibility property to show and hide the content </h4>
<div id="link">
<a id="visibility" href="#" onclick="toggleVisibility();">Click here to see how visibility works</a>
</div>
<br>
<div id="block1">Block 1 (Click on given link above  to see Visibility property using JavaScript) </div>
<div id="block2">Block 2 <br>
</div>
<div id="block3">Block 3</div>
</div>
<script type="text/javascript">
function toggleVisibility()
{
var block1 = document.getElementById('block1');
if(block1.style.visibility == "hidden")
{
block1.style.visibility = "visible";
}
else
{
block1.style.visibility = "hidden";
}
}
</script>
</body>
</html>

Output:

Output with visible value:

visible value Example 1

Output while clicking the link to hide element:

JavaScript Style Visibility Example 1

Example #2

To hide content on button click:

Code:

<!DOCTYPE html>
<html>
<body>
<h4>Hide contents on button click</h4>
<p id="hide_demo">Today's Thought
<br> New day is like Painting, Draw lines with prayers, Erase mistakes with forgiveness, Dip brush with lots of patience and color it with love and Respect
<br><br>
Never design your character like garden where anyone can walk. Design your character like the sky where everyone desire to reach.
<br><br>
Life isn't about finding yourself. It's about creating yourself. </p>
<button type="button" onclick="myFunction()">Click here to hide content </button>
<script>
function myFunction() {
document.getElementById("hide_demo").style.visibility = "hidden";
}
</script>
</body>
</html>

Popular Course in this category
JavaScript Training Program (39 Courses, 23 Projects, 4 Quizzes)39 Online Courses | 23 Hands-on Projects | 225+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (6,106 ratings)
Course Price

View Course

Related Courses
Angular JS Training Program (9 Courses, 7 Projects)Vue JS Training (1 Courses, 3 Project)

Output:

Output before hiding contents:

JavaScript Style Visibility Example 2

Output after hiding contents as follows:

JavaScript Style Visibility Example 2

Example #3

Example to demonstrate space taken by hidden content. 

Code:

<!DOCTYPE html>
<html>
<head>
<style>
h3.hide {
visibility: hidden;
}
h3.show {
visibility: visible;
}
</style>
</head>
<body>
<h2>The visibility Property with attribute value visible and hidden.</h2>
<p>This is example where we are going to show both values as visible and hidden. It's showing difference that the element we are hiding by hidden value its take place on the layout, but showing as blank.</p>
<h3 class="hide">This heading is example of hidden content</h3>
<h3 class="show">This heading is example of shown content</h3>
<p>In the above example we are hiding first hiding still it takes place on the webpage layout.</p>
</body>
</html>

Output:

Hidden Content Example 3

Conclusion

  • From all the above information, we can conclude that style visibility is used to show or hide elements on the screen. If we are going to use it along with JavaScript than we can create a critical menu as well as layouts of very complex WebPages.
  • It’s going to be used with property values like visible, hidden, collapse, inherit as well as initial.

Recommended Articles

This is a guide to JavaScript Style visibility. Here we discuss the Introduction to JavaScript Style visibility and its Examples along with its Code Implementation. You can also go through our other suggested articles to learn more –

  1. JavaScript String Format
  2. Javascript Nested Functions
  3. JavaScript Date Function
  4. JavaScript String replace

JavaScript Training Program (39 Courses, 23 Projects)

39 Online Courses

23 Hands-on Projects

225+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
JavaScript Tutorial
  • Functions
    • JavaScript String Functions
    • JavaScript String Length
    • JavaScript split String
    • JavaScript Math Functions
    • Recursive Function in JavaScript
    • Regular Expressions in JavaScript
    • JavaScript Arrow Function
    • JavaScript Date Function
    • Match Function in Javascript
    • Replace Function in JavaScript
    • JavaScript Call Function
    • JavaScript Pass By Value
    • split() Function in JavaScript
    • reduce() Function JavaScript
    • JavaScript String replace
    • JavaScript Compare Strings
    • JavaScript Sleep
    • JavaScript toLowercase()
    • JavaScript String to Float
    • JavaScript String to Number
    • JavaScript String to int
    • JavaScript Object to String
    • JavaScript Convert to JSON
    • JavaScript Append
    • Javascript Array to String
    • Javascript Nested Functions
    • Set in JavaScript
    • Vectors in JavaScript
    • Javascript Anonymous Function
    • sign() in JavaScript
    • isNaN() JavaScript
    • Slice() Method in JavaScript
    • Javascript void
    • endsWith() in JavaScript
    • trim() Function in JavaScript
    • JavaScript typeof
    • JavaScript indexOf()
    • JavaScript encodeURI()
    • JavaScript Random
    • Ceil() in JavaScript
    • JavaScript tofixed
    • JavaScript hash()
    • JavaScript MD5
    • JavaScript search
    • JavaScript z-index
    • JavaScript Absolute Value
    • JavaScript Closure
    • Javascript Prototype
    • JavaScript Date parse
    • JavaScript DatePicker
    • JavaScript Parse String
    • JavaScript undefined
    • JavaScript FileReader
    • JavaScript Style visibility
    • JavaScript sleep Function
    • JavaScript forEach()
    • JavaScript keys()
    • JavaScript keycodes
    • JavaScript find() 
    • JavaScript values()
    • JavaScript Counter
    • JavaScript Countdown
    • JavaScript instanceof
    • JavaScript Delay
    • JavaScript Default Value
    • JavaScript concat String
    • JavaScript Document Object Model
    • Unshift JavaScript
    • JavaScript Callback Function
    • JavaScript hasOwnProperty()
    • JavaScript UUID
    • JSON Parser
    • JSON Array of Strings
    • Sublime Pretty JSON
    • JavaScript JSON to string
    • JavaScript Uppercase
    • JavaScript Namespace
    • JavaScript Range
    • JavaScript JSON
    • JavaScript exec()
    • JavaScript test()
    • JavaScript Self Invoking Functions
    • JSON Stringify Pretty
    • JavaScript findIndex()
    • JavaScript entries()
    • JavaScript join()
    • JavaScript lastIndexOf()
    • JavaScript every()
    • JavaScript getElementById()
    • JavaScript getElementsByName()
    • JavaScript getElementsByTagName()
    • JavaScript getElementsByClassName()
    • JavaScript Animation
    • JavaScript Minify
  • Basic
    • Introduction To JavaScript
    • What is JavaScript
    • What Javascript Can Do
    • Uses of JavaScript
    • How JavaScript Works
    • Is Javascript Case Sensitive
    • Is Javascript Object Oriented
    • Features of JavaScript
    • Careers in JavaScript
    • How to Install JavaScript
    • Best Javascript Frameworks
    • JavaScript MVC Frameworks
    • JavaScript Tools
    • What is JSON
    • What is Redux
    • What is ES6
    • Uses of React JS
    • How Analytics.JS Works
    • JavaScript Compilers
    • Java and JavaScript
    • JavaScript Literals
    • Variables in JavaScript
    • JavaScript Global Variable
    • JavaScript Keywords
    • String in JavaScript
    • Pointers in JavaScript
    • Primitive Data Types in JavaScript
    • JavaScript console log
    • Object in JavaScript
    • JavaScript Enum
    • Arithmetic in JavaScript
    • Assignment Operator in JavaScript
    • JavaScript Modules
    • Cheat Sheet JavaScript
    • NPM Alternatives
  • Objects
    • JavaScript Objects
    • JavaScript History Object
    • JavaScript Object Notation
    • JavaScript Map Object
    • JavaScript Date Object
    • JavaScript Window Object
    • JavaScript Object Constructors
    • JavaScript Clone Object
    • JavaScript Object.assign()
    • JavaScript object.is()
    • JavaScript Object to JSON
  • Operators
    • Arithmetic Operators in JavaScript
    • JavaScript Assignment Operators
    • Logical Operators in JavaScript
    • Comparison Operators in JavaScript
    • Bitwise Operators in JavaScript
    • Ternary Operator JavaScript
    • Boolean Operators in JavaScript
    • JavaScript?Modulo
    • JavaScript Boolean()
    • Unary Operators in JavaScript
    • JavaScript Number
    • JavaScript Floating
    • JS Operator Precedence
  • Control statements
    • Control Statement in JavaScript
    • Conditional Statements in JavaScript
    • Break Statement in JavaScript
    • Continue in JavaScript
    • Switch Statement in JavaScript
    • Case Statement in JavaScript
    • JavaScript if Statement
    • Nested if in JavaScript
    • JavaScript elseIf
  • Loops
    • For Loop in JavaScript
    • While Loop in JavaScript
    • Do While Loop in JavaScript
    • Nested Loop in JavaScript
  • Array
    • Arrays in JavaScript
    • 2D Arrays in JavaScript
    • Multi-Dimensional Array in JavaScript
    • Associative Array in JavaScript
    • JavaScript Declare Array
    • Arrays Methods in JavaScript
    • JavaScript Loop Array
    • String Array in JavaScript
    • JavaScript Get Array Length
    • JavaScript Merge Arrays
    • JavaScript Array Sort
    • JavaScript Array Push
    • JavaScript Iterate Array
    • JavaScript Empty Array
    • JavaScript Array Concat
    • Dynamic Array in JavaScript
    • JavaScript subarray()
    • JavaScript Array Filter
    • JavaScript Nested Array
    • JavaScript Flatten Array
    • JavaScript Array map()
    • JavaScript Array includes()
    • JavaScript Array Contain
    • JavaScript Array Slice
    • JavaScript Copy Array
    • Javascript Sum Array
    • JavaScript reverse Array
    • JSON Parse Array
  • Sorting
    • Sorting Algorithms in JavaScript
    • Insertion Sort in JavaScript
    • Merge Sort in JavaScript
    • Quick Sort in JavaScript
    • Bubble Sort in JavaScript
    • pop() in JavaScript
    • push() in JavaScript
    • Sort string in JavaScript
  • Events
    • JavaScript Events
    • JavaScript Event Handler
    • JavaScript Keyboard Events
    • JavaScript Mouse Events
    • JavaScript mousemove
    • JavaScript mousedown
    • JavaScript onchange
    • JavaScript onmouseout
    • JavaScript Onkeydown
    • JavaScript onsubmit
    • JavaScript Form Events
    • JavaScript Window Events
    • JavaScript Custom Events
    • JavaScript Alert
    • JavaScript Confirm
    • JavaScript onclick Alert
    • JavaScript Apply
    • JavaScript onblur
    • JavaScript onkeyup
    • JavaScript onfocus
  • Advanced
    • Constructor in JavaScript
    • Inheritance in JavaScript
    • Polymorphism in JavaScript
    • JavaScript Static Method
    • Navigator in JavaScript
    • Abstract Classes in JavaScript
    • keyword in JavaScript
    • Overriding in JavaScript
    • JavaScript Clear Console
    • JavaScript References
    • JavaScript list
    • JavaScript Grid
    • JavaScript innerText
    • Cookies in JavaScript
    • Delete Cookie in JS
    • Encapsulation in JavaScript
    • JavaScript Parent Node
    • JavaScript Parent
    • Javascript Remove Element
    • Errors in JavaScript
    • Forms in JavaScript
    • JavaScript Validator
    • JavaScript Form Validation
    • Email Validation in JavaScript
    • Hamburger Menu JavaScript
    • JavaScript Date Formats
    • JavaScript Number Format
    • JavaScript Debugger
    • JavaScript Stack
    • Queue in JavaScript
    • parseFloat in JavaScript
    • Javascript innerHTML
    • JavaScript setInterval
    • JavaScript Popup Box
    • JavaScript Message Box
    • Lightbox in JavaScript
    • Javascript Design Patterns
    • addEventListener JavaScript
    • Timer in JavaScript
    • JavaScript setTimeout
    • JavaScript clearTimeout()
    • JavaScript String Format
    • JavaScript Scroll to Top
    • JavaScript Function Declaration
    • JavaScript Function Arguments
    • Javascript Strict Mode
    • File Handling in JavaScript
    • JavaScript parseInt
    • JavaScript prompt
    • JavaScript Set Class
    • JavaScript Try Catch
    • Javascript Throw Exception
    • Finally in JavaScript
    • JavaScript Get Element by Class
    • JavaScript Obfuscator
    • Disable JavaScript
    • SetAttribute JavaScript
    • JavaScript Cursor
    • LinkedList in JavaScript
    • JavaScript WeakMap
    • JavaScript DOM
    • JavaScript defer
    • JavaScript Promise
    • Pagination in JavaScript
    • JavaScript Refresh Page
    • JSON.stringify JavaScript
    • JavaScript IIFE
    • JavaScript Fetch API
    • JavaScript Auto Complete
    • JavaScript Copy to Clipboard
    • JavaScript querySelector
    • JavaScript Graph
  • Programs
    • Patterns in JavaScript
    • Reverse in JavaScript
    • Palindrome in JavaScript
    • Factorial Program in JavaScript
    • Fibonacci Series In JavaScript
    • Square Root in JavaScript
    • Prime Number in JavaScript
    • Armstrong Number in JavaScript
    • Random Number Generator in JavaScript
    • Reverse String in JavaScript
    • JavaScript Random String
    • Functional Programming in JavaScript
  • Interview Questions
    • Javascript Interview Questions
    • JSON Interview Questions
    • JS Interview Questions

Related Courses

JavaScript Certification Training

Angular JS Certification Training

Vue JS Training

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - JavaScript Training Program (39 Courses, 23 Projects) Learn More