Advertisement
Advertisement


Get SELECT's value and text in jQuery


Question

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>
2017/05/23
1
37
5/23/2017 12:25:27 PM

Accepted Answer

<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

2014/08/21
90
8/21/2014 3:46:32 AM

$("#yourdropdownid option:selected").text(); // selected option text
$("#yourdropdownid").val(); // selected option value
2012/09/27

$('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

2012/09/27

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

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

& to get the currently selected text:

$('#myDropdownID:selected').text();
2012/09/27

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());
    });
});
2012/09/27