Populating Second Listbox

J

jack-b

Hi,

I have a list box which displays countries names and a second listbox
which displays their cites (based on the selection made in ListBox 1)

If the user selects USA i want to display cities in the States - if the
user selects multiple countries I want to display the cities in ALL the
selected counties e.g. a user select US and England, the second list
would display ally cities in England and all the cities in the US.

I am new to javascript so would appreciate any help.
 
K

Krij

Hi!

I solved a similar situation like this: When user picks an item in
lstCategory, lstItemDetail will be populated by:

With Me!lstItemDetail
.RowSource = ""
.ColumnCount = 4
.ColumnWidths = "0cm;7cm;2cm;0cm"
.BoundColumn = 1
.RowSource = "SELECT ItemID,ItemName,ItemPrice,CategoryID " _
& "FROM tblItems " _
& "WHERE (((CategoryID) = Forms!frmItems!lstCategory)) " _
& "ORDER BY ItemName;"

This was done in ms access 2000. And I suppose that you have country in
one table and cities in another with a relation between them.

Hope this helps :)

(e-mail address removed) skrev:
 
J

jack-b

Hi,
Thanks for your help but I was really looking for a javascript
solution. I have been able to solve this using a postback to the
server.

Can anyone help??
 
D

Dr John Stockton

JRS: In article <[email protected]>,
dated Sun, 2 Jul 2006 23:43:26 remote, seen in
news:comp.lang.javascript, (e-mail address removed) posted :
I have a list box which displays countries names and a second listbox
which displays their cites (based on the selection made in ListBox 1)

If the user selects USA i want to display cities in the States - if the
user selects multiple countries I want to display the cities in ALL the
selected counties e.g. a user select US and England, the second list
would display ally cities in England and all the cities in the US.

Do you consider it reasonable to impose on unsuspecting users, who on
the WWW will not necessarily be using broadband landline, the burden of
downloading the names of all the cities in the world?
 
J

jack-b

It's totally irrelevant on what the values represent! I take it you
don't know the answer? I was actually using the country/city scenario
as an example because it made the question clear.
 
K

Krij

(e-mail address removed) skrev:
It's totally irrelevant on what the values represent! I take it you
don't know the answer? I was actually using the country/city scenario
as an example because it made the question clear.

Sorry....I was in the wrong group and my brain stopped functioning for
a second or two...

:)
 
D

Danny

This is an old example I made:
------------------------
<html>
<head> <title>none </title>


<script type="text/javascript">
var seafood=['shrimp','guppi','lobster','catfish'];
var foodtype=['italian','thai','chinese'];
var hardware=['pci peripherals','agps','cases','keyboards','mice','network cables'];


function getList(el,el2) {

itemChosen=window[el.options[el.selectedIndex].value];
if (el.selectedIndex==0) return; // 1st option in 1stlist is the header
el2.length=0; // to reset the 2nd select

for (i=0;i<itemChosen.length;i++) {
newOpt=new Option(itemChosen,'this is '+itemChosen); // 1st argument is
the text to show, 2nd is the value for it if any
el2.options[el2.length]=newOpt; // adds it
}

}

</script>

</head>
<body style="margin: 4em">

<form>

<select name="upperList" onchange="getList(this,this.form.belowList)">
<option value="">--- choose below ---</option>
<option value="seafood">want seafood </option>
<option value="foodtype">what food type </option>
<option value="hardware">hardware </option>
</select>

<select name="belowList" onchange="alert(this.options[this.selectedIndex].value)">
<option value=""> </option>
</select>

<textarea onkeydown="(this.value.length>=200) ? return true : return false" name="s1"
cols="48" rows="4" ></textarea>
</form>


</body>
</html>

--------------------

bear in mind that I use arrays for the variables to be used to populate the Select "belowList",
now, for UK/US cities populating, that will be a huge array :), and you most likely will break
it down to counties/provinces/locales and such, so, the same applies, using an array, BUT do
asynchronous calls to the server upon the user selecting anything, so the list of the items to
be used, AND inserted as the arrays content, are fetched from the server only on a
user-selection basis, that way you don't preload everything and bogdown the page loading. To
make asynchronous calls check some premade sets on so-called Ajax.

Danny
 

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,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top