Home Ask Login Register

Developers Planet

Your answer is one click away!

Aresof February 2016

Select value loaded with jQuery load function doesn´t appear

I have a select whose values are loaded with load function jQuery:

Html:

<select name="myselect" id="myselect"></select>

Script jQuery:

$(document).ready(function() {
    $("#myselect").load('ajax.php?op=selectYESNO&id=YES');
});

File ajax.php with case:

    case 'selectYESNO':            
        echo '<option value="'.$id.'">'.$id.'</option>
              <option value="YES">YES</option>
              <option value="NO">NO</option>';        
    break; 

Well, my problem is that although the select is set on the form (it takes value YES), if I see its value I get undefined value or null:

$(document).ready(function() {
    $("#myselect").load('ajax.php?op=selectYESNO&id=YES');

    alert($("#myselect option:selected").val());  => undefined
    alert($("#myselect").val());                  => null
}); 

Thanks in advance

Answers


slb February 2016

If you are sure that the data was loaded into the page then try this alert( $(document).find("#myselect option:selected").val() );

instead of : alert($("#myselect option:selected").val());

Post Status

Asked in February 2016
Viewed 1,032 times
Voted 11
Answered 1 times

Search




Leave an answer


Quote of the day: live life