# How to check if an element is hidden or visible using JavaScript

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Jul 30, 2022. Topics: JavaScript.
Canonical URL: https://attacomsian.com/blog/javascript-check-element-hidden-visible

> A quick guide to learning how to check if an element is hidden or visible in DOM using JavaScript.

In JavaScript, the quickest way to check if an element is hidden or visible in DOM is to use the `getComputedStyle()` method. This method returns the [actual values of CSS properties](https://attacomsian.com/blog/javascript-get-css-styles#getting-rendered-styles) used to render an HTML element in DOM.

Let us say that we have got the following hidden HTML element:

```css
.hidden {
    display: none;
}
```

```markup
<button class="hidden">Click Me!</button>
```

An HTML element can be hidden due to either `display:none` or `visibility:hidden` CSS properties. 

Let us write a function that checks both these properties and returns a boolean value depicting the visibility status of the element:

```javascript
const isHidden = elem => {
  const styles = window.getComputedStyle(elem)
  return styles.display === 'none' || styles.visibility === 'hidden'
}

const elem = document.querySelector('button')
if (isHidden(elem)) {
  console.log(`Element is hidden!!`)
} else {
  console.log(`Element is visible!!`)
}
// Element is hidden!!
```

If you are using jQuery, you can use the `:hidden` and `:visible` selectors to check if a [DOM element](https://attacomsian.com/blog/getting-dom-elements-javascript) is hidden or visible:

```javascript
// Check if element is hidden
if ($('button').is(':hidden')) {
  console.log(`Element is hidden!!`)
}

// Check if element is visible
if ($('button').is(':visible')) {
  console.log(`Element is visible!!`)
}
```
