[Javascript] Show and hide multiple div tags

Steve Clay sclay at ufl.edu
Thu May 11 07:27:26 CDT 2006


Wednesday, May 10, 2006, 5:13:13 AM, Abyss - Information wrote:
>   if(show.style.display=="none")

This is a common mistake.
Until you've explicitly set the style property via JS or the style
attribute in the markup, show.style.display will be undefined, not "none"
or any other CSS value you might expect. That's why your current script
takes 2 clicks: The display property first goes from (undefined) to "none"
then finally "block". To suit your purposes, this would work:

show.style.display =
  (!show.style.display || show.style.display=="none")? "block" : "none";


The general way to get a "computed" style requires you to use
window.getComputedStyle or IE/win's currentStyle. Here's one such function:
http://www.somethingleet.com/forum/showthread.php?threadid=35610

Steve
-- 
http://mrclay.org/




More information about the Javascript mailing list