1

I am using jQuery UI autocomplete to select a value from a database. Everything about this is working just fine, with one exception. For the life of me, I cannot get the textbox I am writing the search value in to display the actual value after I have selected it.

I have read people having this problem many times, however the solutions they are presented are not working for me.

The fiddle is here: https://jsfiddle.net/p8y7111p/

The autocomplete code is:

$("#student_search").autocomplete({
  source: "functions/find_student.php",
  delay: 50,
  minLength: 3,
  select: function(event, ui) {
    var name_person = ui.item.label; // this works to give me the name of the person
    var house = ui.item.value.house;
    var id = ui.item.value.id;
    highlightStudent(name_person, id, house);
    $('#student_search').val(name);
  }
});

Note that I have tried:

  • replacing name with ui.label.value

  • trying adding a close or change property to autocomplete, with $('#student_search').val(name) as its value

  • placing the $('#student_search').val(name) outside of the autocomplete

I literally cannot think of anywhere else to put stuff, and so I turn to you all.

Thank you! Alex

6
  • 1
    Your fiddle doesn't work - could you recreate a workable version that produces your error? Commented Jan 10, 2016 at 10:17
  • Where does name come from? Did you mean name_person? Commented Jan 10, 2016 at 10:18
  • 1
    You've not defined anything for the close attribute in the fiddle, which is why there's an error. Either remove the attribute or define a function. Commented Jan 10, 2016 at 10:22
  • Can you add a sample AJAX response? Commented Jan 10, 2016 at 10:23
  • This is the kind of response I am getting: {"item":{"label":"Alexander Bunting","value":{"id":6,"house":"s"}}} Everything that collects values from the response is working, it is literally only that one box that is not using the value correctly. ui.item.label is absolutele returning "Alexander Bunting". name is name_person, they are the same value - my code snipped above does not show this but they have the same value and type. The close attribute is still there by accident, from when I was trying anything to get it to work. It still returns object object without it. Commented Jan 10, 2016 at 12:47

1 Answer 1

2

Have you tried event.preventDefault();?

The default behavior of the select event is to update the input with ui.item.value. This code runs after your event handler.

Simply prevent the default action on select and focus(not done here) using event.preventDefault() or by return false and it will work fine.

$("#student_search").autocomplete({
  source: "functions/find_student.php",
  delay: 50,
  minLength: 3,
  select: function(event, ui) {
    event.preventDefault();
    var name_person = ui.item.label; 
    var house = ui.item.value.house;
    var id = ui.item.value.id;
    highlightStudent(name_person, id, house);
    $('#student_search').val(name_person);
  }
});

Here is a working solution : FIDDLE

Sign up to request clarification or add additional context in comments.

1 Comment

I'm still getting the [object object] when doing a keydown on the items in the dropdown. Any idea?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.