spacer
Yehuda Shiran December 16, 2000
Checking Element Visibility
Tips: December 2000

Yehuda Shiran, Ph.D.
Doc JavaScript

Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs

The browser-independent W3C Standard's way to set and get an element's visibility is via the style object's visibility property. The visibility property may have three possible values:

  • An empty string ("")
  • "hidden"
  • "visible"

An element is visible when its style object's visibility property is either empty ("") or "visible". It is invisible when the property is "hidden".

When you initially place an element, its default visibility is an empty string (""). The following code segment renders two buttons and their onclick event handler:

<FORM>
<INPUT ID="button1" STYLE="position:relative; left:0;" TYPE="button" VALUE="Show Visibility of Other Button" onclick="handleClick()">
<INPUT ID="button2" STYLE="position:relative; left:150;" TYPE="button" VALUE="Show My Visibility" onclick="handleClick()">
</FORM>
<SCRIPT LANGUAGE="JavaScript">
<!--
function handleClick() {
  alert('visibility = "' + document.getElementById('button2').style.visibility + '"');
}
// -->
</SCRIPT>

Click the left button to find out the visibility value of the right button. It should show an empty string::

Now you know how to test if an element is visible or hidden. There are two ways:

if (visibility == "")...

or

if (visibility != "hidden")...

This script does not work on Mac IE4.5.


People who read this tip also read these tips:

Look for similar tips by subject:

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint