473,396 Members | 1,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

adding values to dropdown lists in javascript

I have searched high and low and am posting this question in the hope
that someone out there on the net can help me out. I am using a merge
of ASP and JavaScript.

My goal is to create 2 drop down lists. Both are populated from a
database using ASP, BUT the contents of the second list are dependant
on the selection of the first list. When the user selects the state in
the first list, then only the cities which reside in that state are
displayed in the second dropdown list.

I have this working to a point using the JavaScript command:
document.frmRegistration.cityName.options[0] = new Option("Perth");
The problem with this is that although it adds a display value, each
state has a stateID, which also needs to be included. In essence, the
line of code above, does this: <option>Perth</option>
What I need it to do is this: <option value="1">Perth</option>


How do I get the 'value' field to insert using javascript ?!?!?!?!

If anyone here knows, please PLEASE let me know... otherwise point me
in the right direction.

Thanks
Jul 23 '05 #1
3 7523
In article <53**************************@posting.google.com >,
ev*****************@hotmail.com enlightened us with...

My goal is to create 2 drop down lists. Both are populated from a
database using ASP, BUT the contents of the second list are dependant
on the selection of the first list. When the user selects the state in
the first list, then only the cities which reside in that state are
displayed in the second dropdown list.


Does this help?

http://www.ipwebdesign.net/kaelisSpa...icSelects.html

--
--
~kaeli~
The best part of having kids is giving them back to their
parents.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace

Jul 23 '05 #2
ev*****************@hotmail.com (Evan) wrote in message news:<53**************************@posting.google. com>...
I have searched high and low and am posting this question in the hope
that someone out there on the net can help me out. I am using a merge
of ASP and JavaScript.

My goal is to create 2 drop down lists. Both are populated from a
database using ASP, BUT the contents of the second list are dependant
on the selection of the first list. When the user selects the state in
the first list, then only the cities which reside in that state are
displayed in the second dropdown list.

I have this working to a point using the JavaScript command:
document.frmRegistration.cityName.options[0] = new Option("Perth");


The problem with this is that although it adds a display value, each
state has a stateID, which also needs to be included. In essence, the
line of code above, does this:
<option>Perth</option>


What I need it to do is this:
<option value="1">Perth</option>


How do I get the 'value' field to insert using javascript ?!?!?!?!

If anyone here knows, please PLEASE let me know... otherwise point me
in the right direction.

Thanks


My work is all in IE, so I can only vouch for IE. I think that
document.frmRegistration.cityName.options[0].value="your
value"
seems to work for me.

You get "your value" from the other select box in the usual way
(document.frmRegistration.otherSelectBox.options[document.frmRegistratin.otherSelectBox.selectedInd ex].value.
Jul 23 '05 #3
Evan wrote:
My goal is to create 2 drop down lists. Both are populated from a
database using ASP, BUT the contents of the second list are dependant
on the selection of the first list. When the user selects the state in
the first list, then only the cities which reside in that state are
displayed in the second dropdown list.
I have this working to a point using the JavaScript command:
document.frmRegistration.cityName.options[0] = new Option("Perth");
The problem with this is that although it adds a display value, each
state has a stateID, which also needs to be included. In essence, the
line of code above, does this:
<option>Perth</option>
What I need it to do is this:
<option value="1">Perth</option>


Taking your example as a starting point:

------
document.frmRegistration.cityName.options['0'] = new
Option("Perth","1")
------

However, you 'll probably need to rewrite that if you want 'cityName'
to allow more than 1 city in the field. You could do someting like:

------
document.frmRegistration.cityName.options[document.frmRegistration.cityName.length]
= new Option("Perth","1");
document.frmRegistration.cityName.options[document.frmRegistration.cityName.length]
= new Option("Sydney","2");
------

Now, you would also need to reset 'cityName' before populating it with
the new content:

------
while (document.frmRegistration.cityName.options.length)
document.frmRegistration.cityName.options[0] = null;
------

Hope this helps
Bart
Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Noyb | last post by:
I have a page with two drop-downs, each listing the same employee names and their id's as the values. So they look like: <select name="selection_1"> <option value="12">Curly</option> <option...
0
by: Toonman | last post by:
I have a webpage with a <form> consisting of a large table grid of dropdown lists used to make changes in a database. Some of these dropdown lists have the same value. I'm trying to make it so...
2
by: Chris Becker | last post by:
This is my attempt to rephrase a question I asked earlier that got no response. I suspect it was my poor/unplanned wording. Here is another attempt: I have a form with some drop down lists. I...
6
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
3
by: Jon | last post by:
I am using cascading dropdowns, where the selection in one determines what fills another. In regular ASP I simply loaded a giant dataset into javascript array and when you clicked on one dropdown,...
1
by: Ed Chiu | last post by:
Hi, I have 2 dropdown lists on an ASP.Net page, the first is a list of states of US, the second is City list. When user selects a state, the web page does a postback, create a DB connection and...
2
by: Bruno Alexandre | last post by:
Hi guys, I'm using Javascript to dynamically add to a dropdown values like for ( i=0; i<tv.length; i++ ) { selectDDAvisCtrl.options = new Option(tv, tv); } and I use ...
2
by: Micela | last post by:
Hi, Please help me out in this task. I have a filed in the database of datetime. and 3 dropdown lists in the page. Im using Asp.net with VB coding. The three dropdown lists are 1.for date...
5
by: plumba | last post by:
Hi all. I have two drop down menus, the first a list of Departments, the second a list of Sections. Each Department has a set of Setions, so the Sections dropdown contains complete list of all...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.