It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming JavaScript Forum

assigning and displaying selected select option

assigning and displaying selected select option

Postby rpjd on Wed Dec 30, 2009 3:15 pm

My select menu is in a DIV
Code: Select all
Class="County"
. The select tag has
Code: Select all
onchange="ShowSuburb();"
For test purposes I want to assign the option selected to a variable, then display the variable. I've tried using
Code: Select all
function ShowSuburbs()
{
var county = document.getElementById("County").value;
alert(county);
}

but all I get is an empty alert. Can anyone help with this?
rpjd
 
Posts: 1
Joined: Wed Dec 30, 2009 2:56 pm

Re: assigning and displaying selected select option

Postby harrierdh on Wed Dec 30, 2009 7:17 pm

Code: Select all
<script>
function ShowSuburbs()
{
    var county = document.getElementById("County");
   alert(county.selectedIndex);
   var countyValue = county.options[county.selectedIndex].value; 
   alert(countyValue);
}
</script>
<select name="County" id="County" onChange="ShowSuburbs()">
<option value="red">red</option>
<option value="blue">blue</option>
<option value="green">green</option>
</select>
harrierdh
50+ Club
 
Posts: 51
Joined: Wed Dec 16, 2009 7:04 pm

Re: assigning and displaying selected select option

Postby rangana on Wed Dec 30, 2009 10:05 pm

@harrierdh: This code:
Code: Select all
var countyValue = county.options[county.selectedIndex].value; 


can be written in a less-verbose manner as:
Code: Select all
var countyValue = county.value;
User avatar
rangana
500+ Club
 
Posts: 935
Joined: Wed Feb 27, 2008 5:14 am
Location: Cebu City Philippines


Who is online

Users browsing this forum: No registered users and 4 guests