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

157
Vistas
How to get async in jQuery autocomplete

I am using the jQuery autocomplete plugin. The situation is that the hint data is taken via the GET API method. I can't make the code wait until the end of accepting data from the API.

$('#vehicleBrand').autocomplete({
  source: function(request, response) {
    $('#vehicleBrandValue').val('');
    $.get('api/brands', {
      query: request.term,
      category: $('select[name="vehicleType"]').val()
    }, function(data) {
      response(data)
    })
  },
  select: function(event, ui) {
    $('#vehicleBrandValue').val(ui.item.data);
    event.target.classList.remove('border-danger');
    prepareModels(ui.item.data);
  },
  close: function(event, ui) {
    if (!$('#vehicleBrandValue').val()) {
      alertify.error('Error text here!');
      event.target.value = '';
      event.target.classList.add('border-danger');
      event.target.focus();
    }
  }
});

In the code above, on the source key, I call the get method to get the data.

let brand = data.car_brand;
$('#vehicleBrand').data("ui-autocomplete").search(brand) 
$("#vehicleBrand").data("ui-autocomplete").menu.element[0].firstChild.click()

In the code above I am looking for a car brand and click on it through the script. But the click occurs before the request for data on the source key has time to complete, and the click occurs in the undefined list. Where can I make the click wait for a response from the function by the key source ?

I tried to use Promise and async/await in function by key source, but it didn't work

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

0

Might advise the following:

$('#vehicleBrand').autocomplete({
  source: function(request, response){
    $.getJSON('api/brands', {
      query: request.term,
      category: $('select[name="vehicleType"]').val()
    }, function(data){
      response(data);
    });
  },
  select: function(event, ui) {
    $('#vehicleBrandValue').val(ui.item.value);
    $(this).removeClass('border-danger');
    prepareModels(ui.item.label);
  },
  close: function(event, ui) {
    if (!$('#vehicleBrandValue').val()) {
      alertify.error('Error text here!');
      $(this).val("").addClass('border-danger').focus();
    }
  }
});

Need to also make sure that you have { label, value } pair someplace in your data results.

See more: https://api.jqueryui.com/autocomplete/#option-source

Using $.getJSON() is short hand for a AJAX GET Request that is expecting JSON data as a result.

If you are later using this code:

let brand = data.car_brand;
$('#vehicleBrand').data("ui-autocomplete").search(brand) 
$("#vehicleBrand").data("ui-autocomplete").menu.element[0].firstChild.click()

The code will execute right away, before results have loaded. You may need to delay the trigger using setTimeout since there is not a Promise to execute after. Something like this might work:

$('#vehicleBrand').autocomplete("search", data.car_brand);
setTimeout(function(){
  $("#vehicleBrand").autocomplete("widget").find(".ui-menu-item:eq(0)").trigger("click");
}, 1200);

This give Autocomplete time to load the events before triggering a click event.

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