Check if an element is visible to the user

Description Javascript

This function checks the CSS properties to see if the element is actually visible to the user. Sometimes useful for form validation, to make sure you're not validating a hidden field.

Requires xGetElementById (also available on this site).

The Code Download

  1. function isVisible(e) {
  2.     //returns true is should be visible to user.
  3.     if (typeof e == "string") {
  4.         e = xGetElementById(e);
  5.     }
  6.     while (e.nodeName.toLowerCase() != 'body' && e.style.display.toLowerCase() != 'none' && e.style.visibility.toLowerCase() != 'hidden') {
  7.         e = e.parentNode;
  8.     }
  9.     if (e.nodeName.toLowerCase() == 'body') {
  10.         return true;
  11.     } else{
  12.         return false;
  13.     }
  14. }

Credits

Added by JC on 13th November 2007

Comments

There are no comments about this snippet.

Post Comment Github Markdown Supported

You must be logged in to post a comment.

Login here to post a comment