Alex
Alex

Reputation: 7688

jquery autocomplete select messing data

I can't figure it out why on jquery autocomplete, the select function messes up the data. By that I mean that value = label when it shouldn't.

Console log output for console.log(val);

Object { value="1558825", label="Cree sus propias noticias cliente", icon="http://servidor...News/10_ae4e0.jpg"}

Console log output for console.log(ui.item);

Object { label="Cree sus propias noticias cliente", value="Cree sus propias noticias cliente"}

Code:

$("#search_input").autocomplete({

  source: function(req, add) {

    $.getJSON("do.php", { OP: "news_search", category: cat_id, get: req }, function(results){

      var suggestions = [];

      $.each(results, function(i, val){
        console.log(val);
        suggestions.push(val.label)
      });

      add(suggestions);

    });
  },
  select: function(event, ui){
    console.log(ui.item); // Here value and label is the same, when it shouldn't
    $("#search_input").val(ui.item.label).attr('data-target', ui.item.value);
    return false;
  },
  minLength: 2
});

Any ideas why is that happening?

Upvotes: 1

Views: 404

Answers (1)

Imdad
Imdad

Reputation: 6042

The code suggestions.push(val.label) pushes only label. So you can't get anything other than the label in the ui object

Try to push the val object suggestions.push(val)

Upvotes: 1

Related Questions