Alex Rivera | Logout

JQuery retrieving text instead of value of select list

Asked 2012-04-19T10:20:43.797
12

I need to get the value from a select list but JQuery is returning the text within the options of the select.

I have the following simple code.

<select id="myselect">
   <option selected="selected">All</option>
   <option value="1">One</option>
   <option value="2">Two</option>
</select>

I then use the following JQuery, which I thought would get me the value

var myOption = $('#myselect').val()

but when I look at myOption I get the text of 'One' or 'two'?

Edit
Report

1 Answer

18

update : add val().

console.log($('#myselect').val());

// all option's value
$('#myselect').find('option').each(function(){
    console.log($(this).text());
    console.log($(this).val());
});

// change event
$('#myselect').change(function(){
    console.log($(this).find(':selected').text());
    console.log($(this).find(':selected').val());
});

​ demo : http://jsfiddle.net/yLj4k/3/

answered 2012-04-19T10:31:36.250

Your Answer