Check if field is displayed with JavaScript


When you have dynamic content on your webpage, it is sometimes useful to check if an HTML element is displayed on the screen.
For example, it could be handy to move the focus on a specific field after validation. But what if this field is actually hidden? Does your browser like to move the focus on an hidden field? Well, I can tell you that IE doesn’t like it very much! 😉

There are a lot of solutions on the web to check if a field is displayed or not. However, the solution I propose is probably the simplest:

function isDisplay(obj) {
    return obj.offsetTop > 0;
}

Note that this code has been tested on Google Chrome 18.0, Firefox 3.6, Safari 5.1.5 and IE 7.

, ,


  1. No comments yet.
(will not be published)