Reputation: 1316
I have a form like this
<label>Select country</label>
<select name="country">
<option value="IND" selected="selected">India</option>
<option value="MY">Malaysia</option>
<option value="US">United states</option>
</select>
</label>
I want to get the value of selected country using jquery.
This is my code in jquery document block: My jquery version: jquery-1.10.1.js
alert($('select[name="country"]').find(":selected").val());
alert($('select[name="country"] option:selected').val());
I dont like to call the selector by ID or class. Thanks in advance.
I couldn't write onChange for the country select-box. I need to execute ajax request on blur of the date input field(followup_date).
$('#followup_date').change(function(e) {
if($(this).val()!='')
{
//$('.tmslot').not(this).attr('checked', false);
var slot = $(this).val();
var country_id = $('select[name="country"] option:selected').val();
My problem is I am getting undefined for country_id.
Upvotes: 6
Views: 70574
Reputation: 805
if you want to get the option:
alert($('select[name=country]').find(':selected').text());
Upvotes: 3
Reputation: 6209
Use .val()
on select
element not on selected option
:
$('select[name="country"]').on('change', function(){
alert($(this).val());
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<select name="country">
<option value="IND" selected="selected">India</option>
<option value="MY">Malaysia</option>
<option value="US">United states</option>
</select>
Upvotes: 13
Reputation: 4637
You may need this Demo Here
alert($('select[name="country"]').val());
$('select[name="country"]').change(function(){
alert($(this).val());
});
Upvotes: 2
Reputation: 82231
As per docs,
The .val() method is primarily used to get the values of form elements such as input, select and textarea. In the case of select elements, it returns null when no option is selected and an array containing the value of each selected option when there is at least one and it is possible to select more because the multiple attribute is present.
You need to simply use .val()
with select elements jquery object to get selected option value:
$('select[name="country"]').val()
Upvotes: 8