/    Sign up×
Community /Pin to ProfileBookmark

Show/Hide Tables

I have a combo box with about 11 different options, each resulting in its own set of sub-options. So, if combo box value “1” is selcted, you’ll see “table1” only, but not the others. It’s classic hide/display and it works, but by the time I add in all 11 options, the code is quite long. Can anyone tell me how to accomplish the same thing using fewer lines of code? (Note the example below only includes 3 of the options, not all 11.) Many thanks.

ASP

[CODE]
vFunction= stripquotes(Request.Form(“cboFunction”))
[/CODE]

HTML

[CODE]
<select name=”cboFunction” onChange=”displayFunctions(this);”>
[INDENT]<option></option>
<option value=”1″>Reconcile</option>
<option value=”2″>Sort</option>
<option value=”3″>Controls</option>[/INDENT]
</select>

<!–Reconcile table: Option value 1 –>
<table>
<tbody id=”tblReconcile” name= “tblReconcile” style=”display: <%If vFunction <> 1 Then Response.Write “none”%>”>
<tr><td></td></tr></tbody>
</table>

<!–Sort table: Option value 2 –>
<table>
<tbody id=”tblSort” name= “tblSort” style=”display: <%If vFunction <> 2 Then Response.Write “none”%>”>
<tr><td></td></tr></tbody>
</table>

<!–Controls table: Option value 3 –>
<table>
<tbody id=”tblControls” name= “tblControls” style=”display: <%If vFunction <> 3 Then Response.Write “none”%>”>
<tr><td></td></tr></tbody>
</table>

[/CODE]

Javascript

[CODE]
function displayFunctions(obj)
{
var Function = obj.options[obj.selectedIndex].value;

//Reconcile

if (Function == 1)
{
[INDENT]document.getElementById(“tblReconcile”).style.display = “”;
document.getElementById(“tblSort”).style.display = “none”;
document.getElementById(“tblControls”).style.display = “none”;[/INDENT]
}

//Sort

else if (Function == 2)
{
[INDENT]document.getElementById(“tblSort”).style.display = “”;
document.getElementById(“tblReconcile”).style.display = “none”;
document.getElementById(“tblControls”).style.display = “none”;[/INDENT]
}

//Controls

else if (Function == 3)
{
[INDENT]document.getElementById(“tblControls”).style.display = “”;
document.getElementById(“tblReconcile”).style.display = “none”;
document.getElementById(“tblSort”).style.display = “none”;[/INDENT]
}
}
[/CODE]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@MrNobodyFeb 12.2009 — Create an array of your id values. Then, in your function, loop through your array elements setting each id value to display="none" except the array element which corresponds to the currently selected option. That one set to display="".
Copy linkTweet thisAlerts:
@baldwingrandauthorFeb 12.2009 — Since I've never used arrays before, can you give me an example of what that might look like? Thanks!
Copy linkTweet thisAlerts:
@MrNobodyFeb 12.2009 — [code=html]<script type="text/javascript">
<!-- //
var myids = new Array("tblReconcile", "tblSort", "tblControls");
//
function displayFunctions(obj)
{
var opt = Number(obj.options[obj.selectedIndex].value);
var x, len = myids.length;
for (x=0; x<len; ++x)
{
if ((x+1) == opt)
{
document.getElementById(myids[x]).style.display = "";
}
else
{
document.getElementById(myids[x]).style.display = "none";
}
}
return true;
}
// -->
</script>[/code]
Copy linkTweet thisAlerts:
@baldwingrandauthorFeb 12.2009 — this works. however- i realized not all these values have hidden tables. is there a way to assign values to the tables, so that the appropriate table comes up- instead of just iterating through the list? Thanks.
Copy linkTweet thisAlerts:
@MrNobodyFeb 12.2009 — The values are in your dropdown. Otherwise, not sure what you mean.
Copy linkTweet thisAlerts:
@baldwingrandauthorFeb 12.2009 — I apologize; I figured it out. Thanks for your help. This works really well.
×

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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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