- Instant help with your JavaScript coding problems

Check if element is hidden in JavaScript

Question:
How to check if element is hidden in JavaScript?
Answer:
let el = document.getElementById('container');
let isHidden = el.offsetParent === null;
Description:

The HTMLElement.offsetParent read-only property returns a reference to the element which is the closest (nearest in the containment hierarchy) positioned ancestor element. If there is no positioned ancestor element, the nearest ancestor td, th, table will be returned, or the body if there are no ancestor table elements either.

offsetParent returns null in the following situations:

  • The element or its parent element has the display property set to none .
  • The element has the position property set to fixed (firefox returns ).
  • The element is or .
Share "How to check if element is hidden in JavaScript?"