Show/hide 'div' using JavaScript Show/hide 'div' using JavaScript javascript javascript

Show/hide 'div' using JavaScript


How to show or hide an element:

In order to show or hide an element, manipulate the element's style property. In most cases, you probably just want to change the element's display property:

element.style.display = 'none';           // Hideelement.style.display = 'block';          // Showelement.style.display = 'inline';         // Showelement.style.display = 'inline-block';   // Show

Alternatively, if you would still like the element to occupy space (like if you were to hide a table cell), you could change the element's visibility property instead:

element.style.visibility = 'hidden';      // Hideelement.style.visibility = 'visible';     // Show

Hiding a collection of elements:

If you want to hide a collection of elements, just iterate over each element and change the element's display to none:

function hide (elements) {  elements = elements.length ? elements : [elements];  for (var index = 0; index < elements.length; index++) {    elements[index].style.display = 'none';  }}
// Usage:hide(document.querySelectorAll('.target'));hide(document.querySelector('.target'));hide(document.getElementById('target'));