138

Is it possible to check if an element's CSS display == block or none using JavaScript?

vsync
  • 118,978
  • 58
  • 307
  • 400
Seth
  • 2,043
  • 5
  • 20
  • 23

10 Answers10

171

As sdleihssirhc says below, if the element's display is being inherited or being specified by a CSS rule, you'll need to get its computed style:

return window.getComputedStyle(element, null).display;

Elements have a style property that will tell you what you want, if the style was declared inline or with JavaScript:

console.log(document.getElementById('someIDThatExists').style.display);

will give you a string value.

Aliaksandr Sushkevich
  • 11,550
  • 7
  • 37
  • 44
Dan Davies Brackett
  • 9,811
  • 2
  • 32
  • 54
89

If the style was declared inline or with JavaScript, you can just get at the style object:

return element.style.display === 'block';

Otherwise, you'll have to get the computed style, and there are browser inconsistencies. IE uses a simple currentStyle object, but everyone else uses a method:

return element.currentStyle ? element.currentStyle.display :
                              getComputedStyle(element, null).display;

The null was required in Firefox version 3 and below.

daniels
  • 4,961
  • 2
  • 22
  • 11
sdleihssirhc
  • 42,000
  • 6
  • 53
  • 67
  • should this not be == in this case? – Kai Qing Feb 01 '11 at 18:08
  • @Kai The triple equal doesn't do type coercion. [Crockford explains why](http://javascript.crockford.com/code.html), in the section called "`===` and `!==` Operators." – sdleihssirhc Feb 01 '11 at 18:15
  • That's pretty interesting. Funny how something like this can just escape notice after so many years of programming. I always adopted the suggestion that === was strict boolean. Good to know. – Kai Qing Feb 01 '11 at 18:23
  • 4
    @Kai: There's no problem with using `===` rather than `==` here, but equally there's no advantage either. Both operands are guaranteed to be strings, so both operators perform exactly the same steps. – Tim Down Feb 01 '11 at 18:38
  • It looks like getComputedStyle is supported from IE 9 onwards - http://caniuse.com/getcomputedstyle – Derek Ekins Aug 05 '14 at 09:10
  • Five and a half years after this answer was posted, are there still browser inconsistencies we need to worry about? – hippietrail Jun 04 '16 at 06:46
  • 1
    @hippietrail And almost 10 years later (2019-10-27) there are still problems. Check [what MDN reports](https://developer.mozilla.org/en-US/docs/Web/API/Window/getComputedStyle) – Felipe Alameda A Oct 28 '19 at 03:11
44

For jQuery, do you mean like this?

$('#object').css('display');

You can check it like this:

if($('#object').css('display') === 'block')
{
    //do something
}
else
{
    //something else
}
Ryan Taylor
  • 12,559
  • 2
  • 39
  • 34
Kai Qing
  • 18,793
  • 5
  • 39
  • 57
  • 10
    Avoid over-complicating the answer. I know that jQuery is becoming somewhat of a standard, but there's no reason to add an entire framework just to check an element's display style. – zzzzBov Feb 01 '11 at 18:09
  • 19
    Yeah but I did this because everyone else gave the raw javascript answer, so if he was using jquery but did not specify then there would be some use in the post – Kai Qing Feb 01 '11 at 18:11
22

This answer is not exactly what you want, but it might be useful in some cases. If you know the element has some dimensions when displayed, you can also use this:

var hasDisplayNone = (element.offsetHeight === 0 && element.offsetWidth === 0);

EDIT: Why this might be better than direct check of CSS display property? Because you do not need to check all parent elements. If some parent element has display: none, its children are hidden too but still has element.style.display !== 'none'.

8

yes.

var displayValue = document.getElementById('yourid').style.display;
Victor
  • 4,721
  • 1
  • 26
  • 31
5

Basic JavaScript:

if (document.getElementById("elementId").style.display == 'block') { 
  alert('this Element is block'); 
}
bluish
  • 26,356
  • 27
  • 122
  • 180
Chugworth
  • 480
  • 2
  • 5
3

To find out if it's visible with plain JavaScript, check whether the display property is 'none' (don't check for 'block', it could also be blank or 'inline' and still be visible):

var isVisible = (elt.style.display != "none");

If you are using jQuery, you can use this instead:

var isVisible = $elt.is(":visible");
MarkXA
  • 4,294
  • 20
  • 22
1

You can check it with for example jQuery:

$("#elementID").css('display');

It will return string with information about display property of this element.

Marek Tuchalski
  • 489
  • 2
  • 8
1

I just found the function element.checkVisibility() which is returning false when display: none is set.

Honestly, I cannot find any documentation for it, so I do not know if it is Chrome specific.

Seega
  • 3,001
  • 2
  • 34
  • 48
  • 1
    Seems to be supported by [everything but Safari](https://caniuse.com/mdn-api_element_checkvisibility), which is about typical. – Hashim Aziz Jun 06 '23 at 23:50
0

With pure javascript you can check the style.display property. With jQuery you can use $('#id').css('display')

Joyce Babu
  • 19,602
  • 13
  • 62
  • 97