1

I'm using jquery-ui autocomplete to validate some form inputs. I've got this code :

function addAutoComplete(beacon, url) {
    $(document).ready(function(){
        $('input' + beacon).autocomplete({
            source: url,
            minLength:3,
            select: function(event, ui) {
                var selectedObj = ui.item;
                $(beacon+'_autocomplete_id').val(selectedObj.id);
                return false;
            },
            change: function(event, ui){
                if (ui.item == null){
                           $(beacon+'_autocomplete_id').val(0);
                } else {
                   $(beacon+'_autocomplete_id').val(ui.item.id);
                   }

            }
            });
        });
}       

The goal is simple : associate the "id" value of the datas with an hidden field. When the user's input is not in the source, we put a "0". My problem is that when the user types everything and do not click or make use of the autocomplete, he could write a proper value but it would not be acknowleged as such.

I've seen how to avoid this problem if you store the source in a local array, for instance, but is there a way to do this with the source coming from the url ? In other terms, is there a property that allow me to manipulate the JSON I go from the url without having to code my callback function for the source ?

Thanks in advance !

2
  • Your 'if' and 'else' there do the same thing... Let's say the user types in a valid value, how would you get the equivalent of that value's 'ui.item.id'? Maybe you can use another event handler, like 'onblur' that uses ajax to check if that value is valid, and populate the '$(beacon+'_autocomplete_id')' ... ? what is 'ui.ite.id' anyway? Is it the id of the autocomplete's li element, or something you get from the server ? Commented Apr 27, 2011 at 6:52
  • I corrected the if / else (copied / paste too the code quickly). I know I can do the way you suggest, but since there is already a request to get the source of the autocomplete, I'd like to use those data. Ui.item.id is the "id" value of the selected item among those proposed by the autocomplete. Instead of putting the value to 0 if there is no "ui.item", I need to get the value in the input, and compare it with every data that was read by the autocomplete request. Commented Apr 27, 2011 at 7:29

1 Answer 1

1

Found a solution : using Scott González wonderful autoSelect option. Works perfectly, really great.

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.