Pregunta

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?

¿Fue útil?

Solución

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.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top