Darryl Hein
Darryl Hein

Reputation: 144957

How do I retrieve the jQuery UI autocomplete within a source callback?

I have the following code, setting up a jQuery UI autocomplete:

$('input').autocomplete({
    source : function(request, response) {
        $.getJSON('/path?c_ajax=1', { input_value: request.term }, function(return_data) {
            if (/* check to see if data is valid */) {
                response(return_data.suggestions);
            } else {
                $('input').field_suggest('option', 'disabled', true);
                response(new Array);
            }
        });
    }
});

I'm wondering if there is some other way to get the autocomplete, other than $('input') or similar? My reasoning is that I may have multiple autocompletes for the same field on 1 page and I want to make sure I have the right one.

Edit: Is there is no way of getting the jQuery autocomplete within the source without using the selectors to getting the original input and therefore the autocomplete?

Upvotes: 2

Views: 470

Answers (2)

karim79
karim79

Reputation: 342635

If it is the ordering of the autocompletes that is important, you can use the :eq selector or the .eq method to get one of them at a specific (zero-based) index like this:

// first input on the page
$("input:eq(0)").autocomplete("search", "foo");

// third input in <form id="form">
$("#form input").eq(2).autocomplete("search", "foo");

Upvotes: 2

Linmic
Linmic

Reputation: 791

why not add a specific class or id into the element? like this:

$('input.hello') //<input class="hello" type="text" />
$('input.hello2') //<input class="hello2" type="text" />
$('input#unique') //<input id="unique" type="text" />

cheers

Upvotes: 1

Related Questions