Categories
JavaScript Answers

How to retrieve the display property of a DOM element?

Spread the love

Sometimes, we want to retrieve the display property of a DOM element.

In this article, we’ll look at how to retrieve the display property of a DOM element.

How to retrieve the display property of a DOM element?

To retrieve the display property of a DOM element, we can get it with the window.getComputedStyle method.

For instance, we write:

<div>
  hello
</div>

to add a div.

Then we write:

const div = document.querySelector('div')
const styles = window.getComputedStyle(div)
console.log(styles.display)

to select the div with querySelector.

Then we call window.getComputedStyle with div to return the styles object.

Then we get the display CSS property value with styles.display.

Conclusion

To retrieve the display property of a DOM element, we can get it with the window.getComputedStyle method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *