Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get SELECT's value and text in jQuery [duplicate]

Tags:

jquery

select

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>
like image 459
CL22 Avatar asked Sep 27 '12 04:09

CL22


5 Answers

<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

like image 132
Vishal Suthar Avatar answered Oct 23 '22 14:10

Vishal Suthar


$("#yourdropdownid option:selected").text(); // selected option text
$("#yourdropdownid").val(); // selected option value
like image 24
Zahid Riaz Avatar answered Oct 23 '22 13:10

Zahid Riaz


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

like image 27
Sushanth -- Avatar answered Oct 23 '22 12:10

Sushanth --


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

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

& to get the currently selected text:

$('#myDropdownID:selected').text();
like image 25
Fredy Avatar answered Oct 23 '22 14:10

Fredy


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());
    });
});
like image 30
swapnesh Avatar answered Oct 23 '22 13:10

swapnesh