Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check visibility of an object with JavaScript

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

I tried !object.getAttribute("dislay", "none")... but that doesn't work.

Could somebody help me please?

Thank you!

like image 421
Denise Avatar asked Jan 25 '11 15:01

Denise


3 Answers

function isvisible(obj) {
  return obj.offsetWidth > 0 && obj.offsetHeight > 0;
}

as it's implemented in JQuery.

like image 118
Andrey Avatar answered Sep 21 '22 04:09

Andrey


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

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

If not, you can try these:

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

But this will work only if display:none has been set explicitly on this object.

like image 10
Arnaud Le Blanc Avatar answered Sep 18 '22 04:09

Arnaud Le Blanc


To get value of a display style using javascript you can use:

IE:

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

Others:

document.getElementById('mydiv').getComputedStyle('display')
or
object.getComputedStyle('display')
like image 2
KJYe.Name Avatar answered Sep 21 '22 04:09

KJYe.Name