38

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
CL22
  • 14,026
  • 26
  • 90
  • 153
  • 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

92
<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
  • 16,685
  • 2
  • 55
  • 101
12
$("#yourdropdownid option:selected").text(); // selected option text
$("#yourdropdownid").val(); // selected option value
Zahid Riaz
  • 2,869
  • 2
  • 26
  • 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 --
  • 54,565
  • 8
  • 62
  • 98
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,790
  • 6
  • 26
  • 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
  • 25,390
  • 22
  • 93
  • 124