/    Sign up×
Community /Pin to ProfileBookmark

make object appear onchange using MULTIselect dropdown

Hi,

im a little puzzled here. I have no problems to show a hidden object with a single selection dropdown. But with a multiselect box i have the following issue. If you select only the option that makes the hidden object appear, it shows it, but if you select also any other option along then it doesnt.

My code is like this right now.

[code]

function ShowQuestion(){
var x = document.getElementById(‘open’).value;

if (x == “other”)
{
document.getElementById(‘other’).style.visibility=’visible’

}
else
{
document.getElementById(‘other’).style.visibility=’hidden’
}
}

[/code]

thank you in advance.

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyJan 22.2009 — That's because, a shortcut like this:
[CODE]var x = document.getElementById('open').value;[/CODE]
used to only ever work in IE -- for which IE, of course, received much unjustified ridicule from the sticklers. These days, though, this shortcut seems to have been accepted by more browser manufacturers but I have no idea whether it has become standard or not (and, frankly, it doesn't really matter, either). IE, of course, would also make available the first selection in a multi-select dropdown box. The remainder of the selections had to be retrieved the "old-fashioned" way.

Thus, if you had been using the "old-fashioned" way all along, you would not be having this problem now. So, here ya go:
[CODE]function ShowQuestion()
{
var sel = document.getElementById('open');
var x, len = sel.options.length;
for (x=0; x<len; ++x)
{
if(sel.options[x].selected
&& sel.options[x].value == "other")
{
document.getElementById('other').style.visibility = 'visible';
break;
}
}
if (x==len)
{
document.getElementById('other').style.visibility = 'hidden';
}
return true;
}[/CODE]

Of course, except in strict XHTML, it is also not entirely kosher to be accessing form elements via [B]getElementById[/B] -- but, whatever works (so they say and, then, get criticized for it).
Copy linkTweet thisAlerts:
@supercainauthorJan 22.2009 — Thank you again, it works very nicely in both browsers.
×

Success!

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