How to change the value of a select menu onchange
Posted: Mon Nov 26, 2012 4:07 am
I thought this was going to be a simple problem but it stumped me. Basically what I want to do is alert the id value of the option that is currently being displayed from a select menu. So if I select English, I want "en" to be alerted and if I select Spanish I want "es" alerted. So far with what I have, only "es" gets alerted regardless of which option I select. I guess it is because, the first child of the select menu is automatically chosen according to the code. So how do I make this work the way I want?
I also tried
$("option").change(function(){
alert($(this).attr("id"));
});
but it didn't work
I also tried
$("option").change(function(){
alert($(this).attr("id"));
});
but it didn't work
Code: Select all
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
$(document).ready(function(){
$("select").change(function(){
alert($(this).children().attr("id"));
});
});
</script>
</head>
<body>
<form>
<select id = "lang">
<option id = "es" value = "es">Spanish</option>
<option id = "en" value = "en">English</option>
</select>
</form>
</body>
</html>