lbennet
lbennet

Reputation: 1103

Trying to accomplish something simple with jquery ui autocomplete

I want to use the users entered value into the input being affected by autocomplete's widget, hope the following is self-explanatory of what I want to achieve:

source: $(this).val()

I think $(this).val() won't work because source expects an object?

I'm new to JavaScript, thanks.

$(".myinput").autocomplete({
    source:$(this).val()
});

Upvotes: 1

Views: 71

Answers (1)

KLee1
KLee1

Reputation: 6178

I think you are using the source argument incorrectly for jQuery UI's autocomplete. $(this).val() will get the current value out of the $(this) DOM object (assuming that's what it's pointing to).

Instead the source argument is supposed to be pointing to a source of possible completions for the input. As an example from the jQuery UI website

$(function() {
    var availableTags = ["ActionScript", "AppleScript", "Asp", "BASIC",
        "C", "C++", "Clojure", "COBOL", "ColdFusion", "Erlang"];
    $( "#tags" ).autocomplete({
        source: availableTags
    });
});

As you can see from this example, source points to an array of possible tag values that will be autocompleted when the user starts typing.

To address my new understanding of the OP's question:

To add the user's current entry into the autocomplete, you could use code like the following:

$( "#tags" ).autocomplete("option","source",
    availableTags.concat($("#tags").val()));

This would need to be called every time the input changes (using the keypress event might be helpful for this.)

Upvotes: 1

Related Questions