/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Make a div visible/hidden

Why does’nt work this script?

function ChangeVis(id) {
if (document.getElementById(id).style.visibility=”visible”)
{
document.getElementById(id).style.visibility=”hidden”;
}
else if (document.getElementById(id).style.visibility=”hidden”)
{
document.getElementById(id).style.visibility=”visible”;
}
}

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@bionoidJan 20.2013 — Hi,

In your code you were doing assignments instead of comparisons. Using == or === will perform a comparison check.

[CODE]function ChangeVis(id)
{
if (document.getElementById(id).style.visibility [COLOR="#FF0000"]===[/COLOR] "visible") {
document.getElementById(id).style.visibility = "hidden";
} else
if (document.getElementById(id).style.visibility [COLOR="#FF0000"]===[/COLOR] "hidden") {
document.getElementById(id).style.visibility = "visible";
}
}[/CODE]


Another way it could be done is like so:

[CODE]function ChangeVis(id)
{
var e = document.getElementById(id);
e.style.visibility = (e.style.visibility === 'hidden') ? 'visible' : 'hidden';
}[/CODE]
Copy linkTweet thisAlerts:
@ReFreezedJan 20.2013 — Using === to compare the types is not really needed here. Just use ==
Copy linkTweet thisAlerts:
@LOLMANLOLauthorJan 21.2013 — Thank you both for the useful advise.

You were right, I had to use == instead of =.

Really thank you. ?
×

Success!

Help @LOLMANLOL 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.4,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...