0

I have a function that works in my JQuery autocomplete.

The function itself seems to work, but when selecting from autocomplete list, the line to empty the input is not working.

I re-created the issue here:

http://jsfiddle.net/bv8nr2gp/1/

Everything works, except the line $('#' + x).val('');

UPDATE

I've updated the fiddle to include the missing elements.

If you enter a value in the input field and click Add it works. The value is added to the textarea and span and then removed from the input.

If you enter "one" or "two" in the input and choose it from the autocomplete list, it works apart from clearing the input

4
  • The elements with partial id like _display and _add seems to be missing in your jsfiddle. Please include those and explain in detail how we can test that it's not working Commented Jun 8, 2022 at 7:59
  • See update, just added it to the fiddle and added a further explanation Commented Jun 8, 2022 at 8:03
  • Please include the code in a snippet in the question, not relying on third-party sites. Commented Jun 8, 2022 at 8:44
  • Your issue is that you're clearing the input before the autocomplete has completed. So it goes: auto-complete:select event, your add function:clear input, auto-complete:set input to selected value. You can confirm this by adding a setTimeout around the .val('') jsfiddle.net/dgxk23b6 Commented Jun 8, 2022 at 8:48

1 Answer 1

1

The issue is that you're clearing the input before the autocomplete has completed.

From jquery-ui (with emphasis):

select( event, ui )

Triggered when an item is selected from the menu.
The default action is to replace the text field's value with the value of the selected item.
Cancelling this event prevents the value from being updated, but does not prevent the menu from closing.

The process is:

  • auto-complete select event
  • which calls your add function which clears the input
  • auto-complete then sets the input to the selected value as part of the autocomplete process

You can cancel the autocomplete select so that it doesn't update the input:

    select: function(event, ui) {
      addEmailContact(this.id, ui.item.value);
      event.preventDefault();
    }
Sign up to request clarification or add additional context in comments.

Comments

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.