1

I'm using jQuery 1.8 Autocomplete.

Is there any way of getting the selected object outside of one of the event handlers?

I'm trying to do something like...

 $("#searchUsers").autocomplete({
        source: 'Users/List',
  });

  $("#addUser").click(function() {
       // get the selected user Id
       var item = $("#searchUsers").autocomplete('selected');
       alert(item.Id);
  });

I can't see any way of doing it but it seems a pretty obvious requirement.

2
  • 1
    Wouldn't $("#searchUsers").val() give what you want? Commented Oct 11, 2011 at 15:49
  • 1
    No, I want the object not the value. Commented Oct 11, 2011 at 16:01

4 Answers 4

1

I was running to the same problem. I could get access to the selected item as an object.

$("#searchUsers").data("autocomplete").selectedItem
Sign up to request clarification or add additional context in comments.

Comments

0

The default behavior is that when the item is selected, the value is placed in the text box. So you could get it by just doing $("#searchUsers").val().

If you need more information from the selected item, then you need to pass a select event handler, and within that handler, store the information elsewhere.

1 Comment

That's what I'm doing at the moment but it's hacky and a little buggy. I was hoping there would be a cleaner way to do it.
0

add the select event to your autocomplete function:

search: function (event, ui) {
        $("#searchUsers").val('');
    },
select: function (event, ui) {
   foo = ui.item.label;
    $("#bar").val(ui.item.id);
   baz = (ui.item.JsonField);
}

look at the Events tab here

edit: added the search event to the function. I'm using the search event to clear fields/variables every time the user beings a search

4 Comments

As the question, I don't want an event.
why not? since that's what does what you need it to
Because I have to store the variable somewhere, and then also attach a change event to clear the variable since select only updates when a genuine value is selected. I asked the question specifically because I want to avoid having to do that, and normally jQuery is well thought out enough to provide some mechanism to handle these kinds of things for you.
no because the search event only fires after minLength and delay conditions are satisfied. If you want to do it using events you have to use change as described in my previous comment. Your update shows exactly how this type of approach leads to bugs, and why the library really should provide a simple mechanism for doing this.
0

Old question, but still...

Just define a JavaScript variable, and, in select event, set the object to that variable. For example:

$(function () {
    var object_selected= -1;
    $("#q").autocomplete({
            source: "path/to/jsondata",
            minLength: 3,
            select: function (event, ui) { //This is the event that you need to implement
                $('#q').val(ui.item.value);
                object_selected = ui.item; //This is what you will use later.
                return true;
            }
    });
});

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.