Question

Im filling a 'select' with multiple options with JSON/AJAX javascript and inside "document ready".. Right after i will set the value of the select with:

$('#dropdown').val('99');

Nothing happens, but the function works fine if i place it in the console?

Était-ce utile?

La solution

You likely need to place $('#dropdown').val('99'); in the success callback of .ajax.

e.g.,

$.ajax({
    url: 'google.com',
    success: function(){
        $('#dropdown').val('99');
    }
});

$.ajax is asynchronous, meaning any code directly after it will run before the ajax request is complete. It's likely you're trying to set the value to 99 before it's in the dropdown.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top