/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Simple if else not working. Please help!

Hi, Can anyone tell me why this is not working? I’m just trying to get a div to be made visible or hidden at the click of a link.

<script language=”JavaScript”>
function showhide()
{
var testvar = document.getElementById(“mydiv”).style.visibility
if (testvar = “visible”)
{
document.getElementById(“mydiv”).style.visibility = “hidden”;
}
else
{
document.getElementById(“mydiv”).style.visibility = “visible”;
}
}
</script>

<div id=”mydiv” style=”visibility:visible;”>
Blah blah blah…
</div>

<a href=”javascript:showhide()”>Click here to enable the element<a/>

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@FangJun 06.2008 — compare:if (testvar == "visible")
Copy linkTweet thisAlerts:
@coldscooterauthorJun 06.2008 — Thank you, and i'm a little embarrassed...
Copy linkTweet thisAlerts:
@FangJun 06.2008 — &lt;script type="text/javascript"&gt;
function showhide()
{
var testvar = document.getElementById("mydiv");
testvar.style.visibility=(testvar.style.visibility == "visible")? "hidden" : "visible";
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@coldscooterauthorJun 06.2008 — I have a problem: The first click on the checkbox doesn't change the visibility. You need to click twice for it to start working. Is there anyway to change this?

<script language="JavaScript">

function showhide()

{

var testvar = document.getElementById("mydiv").style.visibility

if (testvar == "visible")

{

document.getElementById("mydiv").style.visibility = "hidden";

}

else

{

document.getElementById("mydiv").style.visibility = "visible";

}

}

</script>

<form>

<input type="checkbox" id="show" onclick="showhide()" checked="checked"> Yes please send me an SMS reminder of the event<br>

</form>


<div id="mydiv">

Mobile number form field

</div>
Copy linkTweet thisAlerts:
@FangJun 06.2008 — It must be explicitly set in the div, else the visibility property returns "" empty:
&lt;div id="mydiv" style="visibility:visible;"&gt;
Copy linkTweet thisAlerts:
@coldscooterauthorJun 06.2008 — Thanks Fang. You legend.
×

Success!

Help @coldscooter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.6,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...