By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,729 Members | 1,343 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,729 IT Pros & Developers. It's quick & easy.

adding values to dropdown lists in javascript

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.