1

I have an array of objects (assessorList), how can I set default text and value in the jquery autocomplete and trigger select. For example if I send second object from the array to the function then Assessor 2, Test should be selected with 1116512 in AutoCompleteAssessorID and all the statements under select should be executed for that selection.

My HTML:

<input style="width:300px" id="AutoCompleteAssessor"> <input type="hidden" id="AutoCompleteAssessorID">

My JavaScript:

var assessorList = [{ id:"1116542",  label:"Assessor 1, Test"}, { id:"1116512",  label:"Assessor 2, Test"}, { id:"1117290",  label:"Carey, Peter"}] 

function a(ID, Text)
{
    $("#AutoCompleteAssessor").autocomplete({ 
        autoFocus: true, 
        minLength: 3,
        source: assessorList,
        focus: function(event, ui){ 
            $("#AutoCompleteAssessor").val(ui.item.label);
            $("#AutoCompleteAssessorID").val(ui.item.id);
            return false;
        },
        select: function(event, ui){                        
            $("#AutoCompleteAssessor").val(ui.item.label);
            $("#AutoCompleteAssessorID").val(ui.item.id);

            userId = ui.item.id;            
            getEventData();                 
            return false;
        },
        change: function (event, ui){
            if(!ui.item)
            { 
                $("#AutoCompleteAssessor").val('');
                $("#AutoCompleteAssessorID").val('');               
            }
        }
    })  
}

I tried $(this).data('ui-autocomplete')._trigger('select', 'autocompleteselect', {item:{value:$(this).val()}}); but i get $(...).data(...) is undefined error.

2
  • Possible duplicate of How do you trigger autocomplete "select" event manually in jQueryUI? Commented Oct 29, 2015 at 15:24
  • I tried $(this).data('ui-autocomplete')._trigger('select', 'autocompleteselect', {item:{value:$(this).val()}}); but I get $(...).data(...) is undefined error. Commented Oct 29, 2015 at 15:28

0

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.