9

I am currently trying to create an autocomplete with a source that is stored in a javascript variable but this variable can be updated by another function. So, what I would like is that at each time the user updates the autocomplete field, the source field of autocomplete is generated.

Here is the code I use:

<head>
    <script>
        var availableTags = ['java', 'javascript']
        // can be called anytime
        var addToTags = function(str){availableTags.push(str)}

        $(function() {
            $( "#tags" ).autocomplete({
                source: availableTags
            });
        });
    </script>
</head>
<body>
    <div class="ui-widget">
        <label for="tags">Tags: </label>
        <input id="tags" />
    </div>
</body>

Do I need to do a callback-like function?

10
  • You should use $(document).ready(). Commented Dec 13, 2012 at 9:11
  • What is sources? And: does it work currently or not? Commented Dec 13, 2012 at 9:12
  • 1
    I think your source needs to be a function, rather than an array. api.jqueryui.com/autocomplete/#option-source Commented Dec 13, 2012 at 9:13
  • 1
    @Bergi I corrected the code in the question that line should be var addToTags = function(str){availableTags.push(str)}. you can pair review it. Commented Dec 13, 2012 at 9:18
  • 1
    @ArashMilani Thanks a lot for the edit, it is exactly what I wanted (copy-paste error). Commented Dec 13, 2012 at 10:09

3 Answers 3

13

a source that is stored in a javascript variable but this variable can be updated by another function.

That should just work. If both the autocomplete and the updating function reference the same array, you can push new values at any time which will be used as soon as the array is evaluated next time (e.g. on keystroke).

I would like that at each time the user updates the autocomplete field, the source field of autocomplete is generated.

That's a different one. Yes, this needs a callback function to generate the source array dynamically, but that's simple. Have a look at the docs:

$( "#tags" ).autocomplete({
    source: function(request, resolve) {
        // fetch new values with request.term
        resolve(availableTags);
    }
});
Sign up to request clarification or add additional context in comments.

Comments

3

Just add a reset call to auto-complete in you addToTags function:

var addToTags = function(str){
   availableTags.push(str);
   $( "#tags" ).autocomplete({
       source: availableTags
   });
}

5 Comments

I might've mentioned this in my own answer, but I was quite sure that this does not work seamlessly when the autocomplete is currently active. Does it?
that is why I addded the destroy statement. This code is equivalent to calling autocomplete on the first time
Yes, it does destroy the currently open suggestions. Have a look at jsfiddle.net/HBtsF and try to use autocomplete there!
Following your comment I've removed the destroy call and tried to push items to the array on each interval. It worked. I've edited my answer accordingly. check it out yourself: jsfiddle.net/HBtsF/1
This answer is nice but I would prefer to manage the autocomplete independently of side effect methods.
1

this is very straight forward

$( "#tags" ).autocomplete('option', 'source', availableTags)

setting availableTags array wherever needed

1 Comment

Thank you! This was exactly what solved running within a Google App Script and google.script.run.

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.