[Javascript] select list option sending

Peter Brunone peter at brunone.com
Thu Oct 9 13:28:51 CDT 2003


David,

   If all you want to do is send the ID back in a form post, you have nothing left to do; the ID attribute of the selected option will be waiting for you back at the server.

   If on the other hand you want to use that ID on the client side before posting, you can access it in the following way (watch for wrapping):

var CityID = selectElement.options[selectElement.selectedIndex].value;

   The CityID variable now contains the value of the currently selected option from the dropdownlist referred to as selectElement.  Yell back if I've totally misunderstood your question...

Cheers,

Peter

Original Message:
>From: "David S Levy" <dlevy at lancer-ins.com>

>Hey.  I'm trying to make a dropdown(select) list of locations in a form, but instead of sending off the location name on submittal, I'd like to send an associated location ID, since I'm working with a database.  I was wondering if I can set up a form element with a name and without a type on selection of a given dropdown option.  To elaborate:
>
>Let's say I have a dropdown list of the following cities:
>
>New York
>Los Angeles
>Boston
>
>And each of these cities has associated with it a location ID, like:
>New York - 17
>Los Angeles - 13
>Boston - 6
>
>I'd like a way to list the cities but send the ID's.  Could I do something within the <option> tag like onselect="this.form.city_ID=17" for New York?  If yes, could I just do it like that or would I need to declare the form element city_ID as a hidden variable or something along those lines?  If not, does anyone have any ideas?  I hope my javascript idea is understandable, I'm just turning to this language in order to move along a project that's primarily in PHP.
>
>Thanks,
>
>David.





More information about the Javascript mailing list