39

Possible Duplicate:
Getting the value of the selected option tag in a select box

For a SELECT box, how do I get the value and text of the selected item in jQuery?

For example,

<option value="value">text</option>
Community
  • 1
  • 1
Jodes
  • 14,118
  • 26
  • 97
  • 156
  • 6
    this should not be marked duplicate. the possible duplicate asked how to get value even though that questioner meant text - this post correctly asks the question how to get the text of a select - i pulled up this question and passed over the other during a search - so marking this a duplicate is not helpful even though the resulting answers were the same because the questions were not. quick to judge != helpful – user1783229 Sep 14 '13 at 13:37

5 Answers5

94
<select id="ddlViewBy">
    <option value="value">text</option>
</select>

JQuery

var txt = $("#ddlViewBy option:selected").text();
var val = $("#ddlViewBy option:selected").val();

JS Fiddle DEMO

Vishal Suthar
  • 17,013
  • 3
  • 59
  • 105
12
$("#yourdropdownid option:selected").text(); // selected option text
$("#yourdropdownid").val(); // selected option value
Zahid Riaz
  • 2,879
  • 3
  • 27
  • 38
9
$('select').val()  // Get's the value

$('select option:selected').val() ; // Get's the value

$('select').find('option:selected').val() ; // Get's the value

$('select option:selected').text()  // Gets you the text of the selected option

Check FIDDLE

Sushanth --
  • 55,259
  • 9
  • 66
  • 105
6

You can do like this, to get the currently selected value:

$('#myDropdownID').val();

& to get the currently selected text:

$('#myDropdownID:selected').text();
Fredy
  • 2,840
  • 6
  • 29
  • 40
4

on the basis of your only jQuery tag :)

HTML

<select id="my-select">
<option value="1">This is text 1</option>
<option value="2">This is text 2</option>
<option value="3">This is text 3</option>
</select>

For text --

$(document).ready(function() {
    $("#my-select").change(function() {
        alert($('#my-select option:selected').html());
    });
});

For value --

$(document).ready(function() {
    $("#my-select").change(function() {
        alert($(this).val());
    });
});
swapnesh
  • 26,318
  • 22
  • 94
  • 126