/    Sign up×
Community /Pin to ProfileBookmark

Show/Hide Multiple DIV IDs Based on Combo Box Values

The following code works, but is not what I’m looking for. This code requires that the value of the combo box be the same as the “suffix” of the div ID. Can someone tell me- can this be re-written so that the DIV ID does NOT have to match the combo box value? I need them to be unique because the option selected in the list won’t always result in the same DIV ID showing. Thanks!

[CODE]
<script type=”text/javascript”>
function showDiv(prefix,chooser)
{
for(var i=0;i<chooser.options.length;i++)
{
var div = document.getElementById(prefix+chooser.options[i].value);
div.style.display = ‘none’;
}

var selectedOption = (chooser.options[chooser.selectedIndex].value);

if(selectedOption == “1”)
{
displayDiv(prefix,”1″);
displayDiv(prefix,”2″);
}
else if(selectedOption == “2”)
{
displayDiv(prefix,”2″);
displayDiv(prefix,”3″);
}
else if(selectedOption == “3”)
{
displayDiv(prefix,”3″);
displayDiv(prefix,”4″);
}
else if(selectedOption == “4”)
{
displayDiv(prefix,”1″);
displayDiv(prefix,”4″);
}

}

function displayDiv(prefix,suffix)
{
var div = document.getElementById(prefix+suffix);
div.style.display = ‘block’;
}

</script>
[/CODE]

[code=html]
<body>

<div id=”body” style=”width:300px;”>
<div>
<form name=”frmOptions”>
<select id=”cboOptions” onChange=”showDiv(‘div’,this)”>
<option value=”1″>Option1</option>
<option value=”2″>Option2</option>
<option value=”3″>Option3</option>
<option value=”4″>Option4</option>
</select>
</div>

<div id=”content” style=”float:right;”>

<div id=”div1″ style=”display:none;”>Test</div>
<div id=”div2″ style=”display:none;”>Test</div>
<div id=”div3″ style=”display:none;”>Test</div>
<div id=”div4″ style=”display:none;”>Test</div>

</div>

</form>
</div>

</body>
[/code]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@vwphillipsFeb 20.2009 — [CODE]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">

function displayDiv(id,sel){
var div = document.getElementById(id+sel.selectedIndex);
if (div) div.style.display = 'block';
}

</script>
</head>

<div id="body" style="width:300px;">
<div>
<form name="frmOptions">
<select id="cboOptions" onChange="displayDiv('div',this)">
<option value="1">Option0</option>
<option value="2">Option1</option>
<option value="3">Option2</option>
<option value="4">Option3</option>
</select>
</div>

<div id="content" style="float:right;">

<div id="div0" style="display:none;">Test 0</div>
<div id="div1" style="display:none;">Test 1</div>
<div id="div2" style="display:none;">Test 2</div>
<div id="div3" style="display:none;">Test 3</div>

</div>

</form>
</div>


</body>

</html>[/CODE]
Copy linkTweet thisAlerts:
@baldwingrandauthorFeb 20.2009 — Thanks. A couple questions:

What if I want to my drop-down selection to display more than one DIV? Say, for example, I select option value "1" from the list, and then want it to display div0 and div1. I imagine there's a for or an If statement of some kind that can be incorporated? Is that possible?

Also- somewhat less crucial, but more curious - I see you still gave the DIV tags numbered IDs - div0, div1. Is it possible to give them names instead? Like divOption1, divOption2, etc.
×

Success!

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