0

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.

asked Sep 22, 2010 at 20:14

2 Answers 2

1

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.

answered Sep 22, 2010 at 20:17
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks ! Oh yeah I forgot I left that part in :)
1

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 :)

answered Sep 22, 2010 at 20:18

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.