How can I get the selected value of a dropdown box using jQuery?
I tried using
var value = $('#dropDownId').val(); and
var value = $('select#dropDownId option:selected').val();
but both returns an empty string.
views:
140answers:
3
+2
A:
For single select dom elements, to get the currently selected value:
$('#dropDownId').val();
To get the currently selected text:
$('#dropDownId :selected').text();
Peter McGrattan
2010-05-06 11:11:23
You need a space before `:selected`.
interjay
2010-05-06 11:15:45
+1 for `.text()`
gnarf
2010-09-09 12:19:19
A:
Did you supply your select-element with an id?
<select id='dropDownId'> ...
Your first statement should work!
schaechtele
2010-05-06 11:15:18
+2
A:
var value = $('#dropDownId').val()
Should work fine, see this example:
<html>
<head>
<script src="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#button1').click(function(){
alert($('#combo').val());
});
});
</script>
</head>
<body>
<select id="combo">
<option value="1">Test 1</option>
<option value="2">Test 2</option>
</select>
<input id="button1" type="button" value="Click!" />
</body>
</html>
Nick Reeve
2010-05-06 11:20:49