Check object visibility using JavaScript

I have a variable called "object". How can I check with JavaScript if it is visible?

I tried! object.getAttribute ("dislay", "none") ... but this does not work.

Can someone help me?

Thank!

+3
source share
6 answers
function isvisible(obj) {
  return obj.offsetWidth > 0 && obj.offsetHeight > 0;
}

since it is implemented in jQuery.

+13
source

If you use jQuery, the following returns true if the object is visible:

$(object).is(':visible');

If not, you can try the following:

if (object.style['display'] != 'none')

But this will only work if display:noneexplicitly set for this object.

+8
source
if (object.style.visibility <> 'visible' || object.style.display == 'none') 

,

 if (object.currentStyle.visibility <> 'visible' || object.currentStyle.display == 'none')
+1

javascript, :

IE:

document.getElementById('mydiv').currentStyle.display
or
object.currentStyle.display

:

document.getElementById('mydiv').getComputedStyle('display')
or
object.getComputedStyle('display')
+1
source

It doesn't look like you are using the getAttribute method correctly. Try a look at this .

0
source

Here is the working version: http://jsfiddle.net/PEA4j/

<html>
<head>
    <script src="http://ajax.aspnetcdn.com/ajax/jquery/jquery-1.4.4.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        $(function () {
            alert("Is #foo1 visible: " + $("#foo1").is(":visible") + "\nIs #foo2 visible: " + $("#foo2").is(":visible"));

        });
    </script>
</head>
<body>
<div id="foo1" style="display:none">foo1 display none</div>
<div id="foo2">foo2 no display property;</div>
</body>
</html>
0
source

Source: https://habr.com/ru/post/1787732/


All Articles