views:

26

answers:

2

Hi, I have a php script with an array which loops through months and displays them in a drop down. I want to get the selected value from the drop down using javascript.

function month_list()
{


 $months = Array("January", "February", "March", "April", "May", "June", "July", 
 "August", "September", "October", "November", "December");

 echo '<select name="month" id="month_list" OnChange="getvalue();">'; 

 foreach ($months as $months => $month)
 {
         echo'<option OnChange="getvalue();" value='.$month.'>'.$month.'</option>';

     }

 echo '</select>';

}

Javascript:

 <script type="text/javascript">


 function getvalue()
 {
 alert(document.getElementById((month_list)).value); 
 }

</script>

Nothing happends when I select a value but in firebug I get the error:

Element referenced by ID/NAME in the global scope. Use W3C standard document.getElementById() instead.

Any advice?

Thanks.

+1  A: 

You forgot to quote month_list.

 document.getElementById("month_list").value

Also you don't need the onchange (that's all lowercase btw) attributes on the individual options, just on the select element.

Matti Virkkunen
Thanks ! Oh yeah I forgot I left that part in :)
Elliott
+1  A: 

Use

function getvalue()
{
alert(document.getElementById('month_list').value); 
}

</script>

Instead.

Also, you can remove the onChange on each of the options, the select will do :)

yorick