Dependent listboxes

S

Simon Templar

I need the following functionality: With 2 listboxes populated from a
database with the SAME data, I need any of the listboxes to stop displaying
the option when selected at the other listbox. Eg: data records are A and B,
and initially are available in both listboxes, but once the user selects A
in the first listbox, the second one should only display B.

Any reference?
 
M

McKirahan

Simon Templar said:
I need the following functionality: With 2 listboxes populated from a
database with the SAME data, I need any of the listboxes to stop displaying
the option when selected at the other listbox. Eg: data records are A and B,
and initially are available in both listboxes, but once the user selects A
in the first listbox, the second one should only display B.

Any reference?

Will this help? Watch for word-wrap.

<html>
<head>
<title>selnodup.htm</title>
<script type="text/javascript">
function noDuplicate() {
var form = document.forms[0];
var valu = form.Sel1.options[form.Sel1.selectedIndex].value;
form.Sel2.options[valu] = null;
}
</script>
</head>
<body>
<form>
<b>From :</b>
<select name="Sel1" onchange="noDuplicate()">
<option value="">
<option value="1">Chicago
<option value="2">New York
</select>
&nbsp; &nbsp;
<b>To :</b>
<select name="Sel2">
<option value="">
<option value="1">Chicago
<option value="2">New York
</select>
</form>
</body>
</html>
 
S

Simon Templar

This code works fine. The only problem appears when the user changes their
selection. The second listbox does not show the hidden option back. I will
try to improve that.

Thanks!

McKirahan said:
Simon Templar said:
I need the following functionality: With 2 listboxes populated from a
database with the SAME data, I need any of the listboxes to stop displaying
the option when selected at the other listbox. Eg: data records are A
and
B,
and initially are available in both listboxes, but once the user selects A
in the first listbox, the second one should only display B.

Any reference?

Will this help? Watch for word-wrap.

<html>
<head>
<title>selnodup.htm</title>
<script type="text/javascript">
function noDuplicate() {
var form = document.forms[0];
var valu = form.Sel1.options[form.Sel1.selectedIndex].value;
form.Sel2.options[valu] = null;
}
</script>
</head>
<body>
<form>
<b>From :</b>
<select name="Sel1" onchange="noDuplicate()">
<option value="">
<option value="1">Chicago
<option value="2">New York
</select>
&nbsp; &nbsp;
<b>To :</b>
<select name="Sel2">
<option value="">
<option value="1">Chicago
<option value="2">New York
</select>
</form>
</body>
</html>
 
M

McKirahan

Simon Templar said:
This code works fine. The only problem appears when the user changes their
selection. The second listbox does not show the hidden option back. I will
try to improve that.

Thanks!

[snip]

Try this; watch for word-wrap.

<html>
<head>
<title>selnodups.htm</title>
<script type="text/javascript">
function selects(what) {
var form = document.forms[0];
var opts = new Array;
opts[0] = "";
opts[1] = "Chicago";
opts[2] = "New York";
if (what == 0) {
form.Sel1.options.length = 0;
}
form.Sel2.options.length = 0;
for (var i=0; i<opts.length; i++) {
if (what == 0) {
form.Sel1.options = new Option(opts, i);
}
form.Sel2.options = new Option(opts, i);
}
}
function noDuplicate() {
var form = document.forms[0];
selects(1);
var valu = form.Sel1.options[form.Sel1.selectedIndex].value;
form.Sel2.options[valu] = null;
}
</script>
</head>
<body onload="selects(0)">
<form>
<b>From :</b>
<select name="Sel1" onchange="noDuplicate()">
</select>
&nbsp; &nbsp;
<b>To :</b>
<select name="Sel2">
</select>
</form>
</body>
</html>

This link may help: http://www.quirksmode.org/js/options.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top