Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

159
Vistas
Problem selecting an item after building the select options with AJAX

I use this javascript to select a specific option (the option value being specified within a hidden element):

$("select").each(function() {
    var id = $(this).attr('id');
    var source = 'input:hidden[name=select_'+id+']';
    if ($(source).length) {
        var selected = $(source).val();
        $(this).val(selected).change();
    }
});

This works fine when the options are hard coded in the HTML source.

I now need to populate the options with an AJAX call, I use the below method:

select : function(ctrl,id) {
    var call = '/'+ctrl+'/'+$("#auth input[name=verify]").val();
    $.getJSON(call, function(result) {
        $.each(result, function() {
            $('#'+id).append($("<option />").val(this.id).text(this.title));
        });
    });
},

I process the select method (AJAX) on page load, and the options populate fine. But when I then try to select the desired option, the browser defaults to the first option.

I have tested what is happening by sticking some alerts around the code as thus:

    alert($(this).val(selected));       // A
    alert($(this).val());               // B
    $(this).val(selected).change();
    alert($(this).val());               // C

When the options are hard coded I get A=3, B=null, C=3 i.e. it works When the options are populated via AJAX I get A=3, B=null, C=null i.e. it fails

I am guessing that I need to trigger some kind of change() event after populating the option list with AJAX. I have tried (a bit overkill I know):

$('#'+id).append($("<option />").val(this.id).text(this.title).change());

&

$('#'+id).append($("<option />").val(this.id).text(this.title)).change();

Any ideas? Thx

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Problem solved.

Although I was triggering the code in the correct order (in theory), because of javascripts event driven behaviour the AJAX call was not completing until after my select initialisation had finished. So I moved the code to set the selected option into the AJAX call and voila.

select : function(ctrl,id) {
var call = '/'+ctrl+'/'+$("#auth input[name=verify]").val();
$.getJSON(call, function(result) {
    $.each(result, function() {
        $('#'+id).append($("<option />").val(this.id).text(this.title));
    });
    var source = 'input:hidden[name=select_'+id+']';
    if ($(source).length) {
        var selected = $(source).val();
        $('#'+id).val(selected).change();
    }
});
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda