• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2140
  • Last Modified:

Dynamic Select Box values from a Multiple Select Box

Hi, I am new here and let me open with the question which is the reason I joined this site:

I have a selection box multiple with countries which supports multiple selections being made. Depending on the Country or Countries selected in the countries box I want to change the options in the State box. I have got it working when only one Country is selected. Problem is that the State values are reset when I select multiple countries.

Is there a way to get the following: A client selects the USA from Countries. The State box is filled with the US states. Now when the client also selects France from Countries the list of US States remains and the French States are added giving me a list with both the US and the French States?

I have looked everywhere but cannot find this being done anywhere.
0
pgkooijman
Asked:
pgkooijman
  • 7
  • 6
  • 3
  • +1
1 Solution
 
XxavierCommented:
eg

<html>
<head>
<script language=javascript>
var m_arr = new Array();
m_arr[0] = "blah 1|blah 2";
m_arr[1] = "blah 3|blah 4";


function sh(obj, o_objn)
{
    f = obj.form;
    other_obj = f[o_objn];
     s = m_arr[obj.selectedIndex].split("|");
     for(i=0;i<s.length;i++)
     {
          other_obj.options[i] = new Option(s[i], s[i]);
     }
}
</script>
</head>
<body>
<form>
<select onChange="sh(this, 'otherselect')">
<option>menu 1</option>
<option>menu 2</option>
</select>

<select name="otherselect">
</select>
</form>
</body>
</html>
0
 
pgkooijmanAuthor Commented:
Thanks for the quick answer Xxavier, thinks work fast here it seems :)

I will give that a try but will that work with multiple selections? It should place the states for all the selected countries in the first selection box. Not replace them each time I click on another country.

Will get back to you.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
XxavierCommented:
be back in an hour customer come in.
0
 
pgkooijmanAuthor Commented:
Like I expected that solution makes the States come up only for one Country. What makes this problem tricky is the support of multiple selections. Re-read my original question please.

To further illustrate using Xxavier's example: When both the first and second option from the first box are selected then the second box should give: blah 1,blah 2,blah 3,blah 4
At this moment it depends on what I click last which two blahs are shown.
0
 
pgkooijmanAuthor Commented:
A possible solution I see would be to retrieve the values of all selected choices in the first selection box and loop through those adding the values for the second box as we go along. Just no clue on how to write such a thing....
0
 
XxavierCommented:
ok try this

<html>

<head><script language="javascript">
Countries = new Array('Greece$Athens$Patras$Larisa','Italy$Rome$Milano$Valencia','Spain$Madrite$Barchelona$Balero')

function fillCountries(){
document.form1.country.length = 1;
document.form1.country.options[0].value = "none";
document.form1.country.options[0].text = "(select)";
document.form1.country.selectedIndex = 0;
for(var i = 0; i < 3; i++){
var k = Countries[i].indexOf('$');
document.form1.country.options[i].value = i + 1;
document.form1.country.options[i].text = Countries[i].substring(0,k);
document.form1.country.length++;
}
document.form1.country.length = document.form1.country.length - 1;
}

function setStates(){
document.form1.states.length = 1;
document.form1.states.options[0].value = "none";
document.form1.states.options[0].text = "(select)";
document.form1.states.selectedIndex = 0;
var state = Countries[document.form1.country.selectedIndex];
var i = 0;
while(true){
var k = state.lastIndexOf('$');
if(k==-1)
break;
choice = state.substring(k+1,state.length);
state = state.substring(0,k);

document.form1.states.options[i].value = i + 1;
document.form1.states.options[i].text = choice;
document.form1.states.length++;
i++
}
document.form1.states.length = document.form1.states.length - 1;

}

</script>

<title></title>
</head>

<body onload="fillCountries()">

<form name="form1">
  <p>Countries<br>
  <select name="country" onChange="setStates()" size="1">
    <option value="none">(select)</option>
  </select> <br>
  States <br>
  <select name="states" size="1">
    <option value="none">(select)</option>
  </select> </p>
</form>
</body>
</html>
0
 
pgkooijmanAuthor Commented:
Xxavier, tried that. Still when I select multiple of those three countries it still only gives me the states of 1 option not all the selected countries.
0
 
devicCommented:
hi pgkooijman,

try this:
==========================================
<html>
<body>
<script>
Countries = new Array(
      new Array("Greece","Athens","Patras","Larisa"),
      new Array("Italy","Rome","Milano","Valencia"),
      new Array("Spain","Madrid","Barselona","Balero")
)
function fillcountry(f)
{
      f.city.length=null;
      for(var i=0; i<f.country.length;i++)
      {
            if(f.country[i].selected)
            {
                  for(var j=0;j<Countries[f.country[i].value*1-1].length-1;j++)
                  {
                        f.city.length++;
                        f.city.options[f.city.length-1].text=Countries[f.country[i].value*1-1][j+1]
                  }
            }
      }
}
</script>
<form>
      <select onchange=fillcountry(this.form) name=country multiple size=5>
            <option value=1>Greece
            <option value=2>Italy
            <option value=3>Spain
      </select>
      <select name=city>
            <option>select city
      </select>
</form>

</body>
</html>
0
 
VincentPugliaCommented:
Hi,
here you go:

<html><head>
<script>
var usa= new Array("NY","CA","AZ","BK")
var france = new Array("Lyons","Paris","Orleans")
var canada = new Array("Ottawa","Ontario","British Columbia")

function doit(formObj)
{
  selObj =formObj.countries
  destObj = formObj.states
  for (var i = 0; i < selObj.options.length; i++)
  {
    if (selObj.options[i].selected)
    {
       arrName = selObj.options[i].value
       for (j = 0; j < window[arrName].length; j++)
       destObj.options[destObj.length] = new Option(window[arrName][j],window[arrName][j])
    }
  }
}
</script>
</head>
<body>
<form name='a'>
<select name="countries" multiple>
<option value="usa">USA</option>
<option value="france">France</option>
<option value="canada">Canada</option>
</option>
</select>
<select name="states">
</select>
<input type="button" onclick=doit(this.form)>
</form>
</body>
</html>

  Vinny
0
 
XxavierCommented:
<html>

<head><script>
Canada=new Array ("Bc","Alta","Sask","Mb","Ont")
USA =new Array ("Al","Ark","Cal","Md","Tx")
function update(obj,val)
{l=document.f1.mySelect.options.length
obj=document.f1.mySelect
switch (val)
 {
      case "USA" :
     
      for (i=0;i<=4;i++)
             obj.options[obj.options.length] =new Option(USA[i],USA[i])
 
break;
case "Canada" :
 for (i=0;i<=4;i++)
             obj.options[obj.options.length] =new Option(Canada[i],Canada[i])

break;
 
default :
 }
}
</script>

<title></title>
</head>

<body>

<form name="f1">
  <p><select name="mySelect" size="1">
    <option value="......">....</option>
  </select></p>
  <p><select size="1" onchange="update(this,this.value)">
    <option value="USA">USA</option>
    <option value="Canada">Canada</option>
    <option selected value="&lt;select&gt;">&lt;select&gt;</option>
  </select></p>
</form>
</body>
</html>
0
 
XxavierCommented:
sorry no need for tht obj param in     function update(obj,val)
0
 
pgkooijmanAuthor Commented:
Thanks Guys for all the input!

I tried them all and only Devic's solution seems to work like it is supposed to. My only question before I rap this question up: will this work on all browsers and if not which are excluded? I tested it on IE6 and NS7. How far back can we go before this stops working?

Thanks again guys, this platform finally seems to be what I am looking for!

0
 
XxavierCommented:
here is the code tidied up a bit

<html>

<head><script>
Canada=new Array ("Bc","Alta","Sask","Mb","Ont")
USA =new Array ("Al","Ark","Cal","Md","Tx")

function update(val)
{
obj=document.f1.mySelect
switch (val)
 {
      case "USA" :
     
      for (i=0;i<=USA.length-1;i++)
             obj.options[obj.options.length] =new Option(USA[i],USA[i])
 
      break;
      case "Canada" :
      for (i=0;i<=Canada.length-1;i++)
             obj.options[obj.options.length] =new Option(Canada[i],Canada[i])

break;
 
default :
 }
}
</script>

<title></title>
</head>

<body>

<form name="f1">
  <p><select name="mySelect" size="1">
  </select></p>
  <p><select size="1" onchange="update(this.value)">
    <option>Select</option>
    <option value="USA">USA</option>
    <option value="Canada">Canada</option>
  </select></p>
</form>
</body>
</html>
0
 
devicCommented:
tested on:
IE 6
Mozilla 1.4
Netscape 7.02
Opera 7.11

and has nothing to be difficult for browsers ;)
0
 
VincentPugliaCommented:
Hi

devic's will work for all, though you may not be thrilled with the way it looks in NN4

correction for my code:
function doit(formObj)
{
  selObj =formObj.countries
  destObj = formObj.states
  destObj.length = 0;

Vinny
0
 
devicCommented:
after this line:
f.city.options[f.city.length-1].text=Countries[f.country[i].value*1-1][j+1];

add this line:
f.city.options[f.city.length-1].value=Countries[f.country[i].value*1-1][j+1];

it was test and i forgot to insert value ;)
0
 
pgkooijmanAuthor Commented:
Ok, thanks Devic and other guys. I have accepted Devic's answer. I am new so I hope this was how the system is meant to be used.....
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

  • 7
  • 6
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now