18

I am wondering how to grab the selected item's text value on jquery autocomplete.

I have initialised jquery as following :

$(document).ready(function (){
    $("input#autocomplete").autocomplete({
        source: postcodelist,
        select: function (event, ui) {
            AutoCompleteSelectHandler(event, ui)
        }
    });
});

And I have created a function function AutoCompleteSelectHandler(event, ui) { }.

Inside this function I want to some extra handling to feed data into correct textboxes but I can't figure out how to grab the text value of the selected item.

I did google a bit and tried examples but I can't seem to get it working.

Any help will be much appreciated.

Thanks a lot advance.

1
  • 1
    If you post the html markup and exactly what you need to do we could help Commented Jun 21, 2011 at 8:21

4 Answers 4

31

The ui parameter has an item property with the selected text

function AutoCompleteSelectHandler(event, ui)
{               
    var selectedObj = ui.item;              
    alert(selectedObj.value);
}

source: http://jqueryui.com/demos/autocomplete/#event-select go to tab "Events" and then event "Select"

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

1 Comment

What would be the full implementation of this?
15
// list of clients
        $( "#client" ).autocomplete({
        source: "lib/getClientList.php",
        minLength: 2,
        select: function(event, ui) {
            alert(ui.item.value);
             }
        })

;

The ui.item.value is the reference that jquery utilizes to assign a value to the input

1 Comment

Is it possible to get index of selected item?
4

You don't need to apply an anonymous function as a wrap, you can directly pass the function ref.

$(document).ready(function (){
   $("input#autocomplete").autocomplete({
        source: postcodelist,
        select: AutoCompleteSelectHandler
    });
});

Within that method, you can either access this or event.target to get the current value, both values are referencing the input element:

function AutoCompleteSelectHandler(event, ui) {
    alert( $(event.target).val() );
    alert( $(this).val() );

    // alert( this.value );
}

2 Comments

why not just get it from the ui parameter that is passed into the method like in my answer as written in jquery ui's documentation?
aww I didn't know that you don't have to specify parameter. Thanks for the tip!
2

You just grab the value from the input in the same way you would if the user had typed it in themself:

$('input#autocomplete').val()

2 Comments

This will give you what the user typed, not the selected value.
Maybe I misunderstood your question? I thought you were looking for the option the user selected with autocomplete. Once they select, it puts it in the input and it's retrievable in the same way as if the user typed it.

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.