Search code examples
jqueryjquery-selectbox

get selectbox selected value by name tag jquery


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.


Solution

  • 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>