Posted almost 9 years ago. Visible to the public.

Check whether an element is visible or hidden with Javascript


You can say:




jQuery considers an element to be visible if it consumes space in the document. For most purposes, this is exactly what you want.

Native DOM API

Emulate jQuery's implementation :

element.offsetWidth > 0 && element.offsetHeight > 0;


Don't use Element#visible(), it just checks if a CSS attribute display: none exists. This is to naive for most cases.

Emulate jQuery's implementation instead:

element.offsetWidth > 0 && element.offsetHeight > 0;

Or maybe you'd even like to patch Prototype so it behaves like jQuery:

Element.addMethods({ visible: function() { return offsetWidth > 0 && offsetHeight > 0; } });

makandra has been working exclusively with Ruby on Rails since 2007. Our laser focus on a single technology has made us a leader in this space.

Owner of this card:

Henning Koch
Last edit:
about 2 years ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Henning Koch to makandra dev
This website uses cookies to improve usability and analyze traffic.
Accept or learn more