/    Sign up×
Community /Pin to ProfileBookmark

Shorten code

I have the following code. Isn’t there way to shorten it in order not to repeat reduntand 99% of the code lines?

[code=php]
function HiddenArea(){
if(document.myForm.option.value != ‘noneSelected’){
document.getElementById(‘hide1’).style.display = “”;
document.getElementById(‘hide2’).style.display = “”;
document.getElementById(‘hide3’).style.display = “”;
document.getElementById(‘hide4’).style.display = “”;
document.getElementById(‘hide5’).style.display = “”;
document.getElementById(‘hide6’).style.display = “”;
document.getElementById(‘hide7’).style.display = “”;
document.getElementById(‘hide8’).style.display = “”;
}
else {
document.getElementById(‘hide1’).style.display = “none”;
document.getElementById(‘hide2’).style.display = “none”;
document.getElementById(‘hide3’).style.display = “none”;
document.getElementById(‘hide4’).style.display = “none”;
document.getElementById(‘hide5’).style.display = “none”;
document.getElementById(‘hide6’).style.display = “none”;
document.getElementById(‘hide7’).style.display = “none”;
document.getElementById(‘hide8’).style.display = “none”;
}
}
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@CharlesNov 15.2006 — This ought to do it:function HiddenArea(){
var e, i = 1
while (e = document.getElementById ('hide' + i++)) {
e.style.display = document.myForm.option.value == 'noneSelected' ? 'none' : ''
}
}
It's not the fastest running version but I don't think that you will notice.
Copy linkTweet thisAlerts:
@santaauthorNov 15.2006 — Wow! Works like a charm! Thanks!!!
×

Success!

Help @santa 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,
)...