Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-16-2012, 08:34 PM   PM User | #1
toddmanning
New to the CF scene

 
Join Date: May 2012
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
toddmanning is an unknown quantity at this point
Can't read css visibility value

I'm not sure why this code executes the first if condition. What should I be doing instead?

<!DOCTYPE html>
<head>

<style>
#test { visibility:hidden; }
</style>

<script type="text/javascript">

function read(ID)
{
if(document.getElementById(ID).style.visibility == '') alert("why does this pop up?");
else if(document.getElementById(ID).style.visibility == 'hidden') alert("this is what I would expect");
else alert("never reached");
}
</script>


</head>
<body>
<form><input type="button" value="test" onclick="read('test');"">
<p id='test'> testing visibility</p>
</body>
</html>
toddmanning is offline   Reply With Quote
Old 05-16-2012, 08:42 PM   PM User | #2
xelawho
Senior Coder

 
xelawho's Avatar
 
Join Date: Nov 2010
Posts: 2,437
Thanks: 52
Thanked 454 Times in 452 Posts
xelawho will become famous soon enoughxelawho will become famous soon enough
javascript cannot read css values unless they are defined inline or by the javascript. I don't know why. It's annoying. There are a couple of ways around it, probably simplest here being to define the style inline:
Code:
<!DOCTYPE html>
<head>

<style>
</style>

<script type="text/javascript">

function read(ID)
{
if(document.getElementById(ID).style.visibility == '') {alert("why does this pop up?");}
else if(document.getElementById(ID).style.visibility == 'hidden') {alert("this is what I would expect");}
else {alert("never reached");}
}
</script>


</head>
<body>
<form><input type="button" value="test" onclick="read('test');"">
<p id='test' style="visibility:hidden;" > testing visibility</p>
</body>
</html>
xelawho is offline   Reply With Quote
Old 05-16-2012, 08:54 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
What most people do, when they need to flip-flop between two states (e.g, in your case, presumably between hidden and visible) is to look for the NON-DEFAULT state.

So if the CSS default is, as in your case, hidden, then in the JS code you would do something like this:
Code:
var para = document.getElementById("test");
para.stye.visibility = ( para.stye.visibility == "visible" ? "hidden" : "visible" );
This works because the default is seen as just "" so the first flip-flop changes it to "visible" and from then on the value has been set by JS code and so is available.

Clearly, if you don't know ahead of time which is the default, then this can't work.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:14 PM.


Advertisement
Log in to turn off these ads.