Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get drop down choice 1

Status
Not open for further replies.

TStriker

Programmer
Nov 12, 2003
277
US
Hello all,

This must be a deadly simple question but I'm such a javascript newbie that I don't know how this is done.

Basically I want my javascript function to use the value that was chosen in a drop down box. Here's the code:

<script type="text/javascript">
function chgCity() {
alert (*** I want to show which city was chosen here ***);
}
</script>


<select id="city" name="city" onChange="chgCity()">
<option value"New York">New York</option>
<option value"Chicago">Chicago</option>
<option value"Denver">Denver</option>
</select>

Thanks,

-Striker
 
You can use something like this:

Code:
function chgCity(cityOption) {
   alert ('You selected ' + cityOption);
}

...

<select id="city" name="city" onChange="chgCity(this.value);">

Although, I normally use 'this.options[this.selectedIndex]' instead of 'this.value' after checking to make sure 'this.selectedIndex' isn't < 0.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Thanks for the help Dan.

No joy. I think I have the basic function done properly because it displays the alert box with a value when I do this:

onChange="chgCity('Boston')"

But when I use:

onChange="chgCity(this.value);"

the alert box comes up but displays nothing.

-Striker
 
onchange="showMeTheMoney(this);"

function showMeTheMoney(selectCtrl)
{
//use the list item value
var selValue = selectCtrl.options[selectCtrl.selectedIndex].value;

//or uncomment to use the displayed text
//var selValue = selectCtrl.options[selectCtrl.selectedIndex].text;

alert("You Have Chosen: " + selValue);
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top