tags:

views:

140

answers:

3

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.

+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
You need a space before `:selected`.
interjay
+1 for `.text()`
gnarf
A: 

Did you supply your select-element with an id?

<select id='dropDownId'> ...

Your first statement should work!

schaechtele
+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
http://www.jsfiddle.net/Fa9Hs/ -- much smaller "example"
gnarf