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?
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).
That’s a different one. Yes, this needs a callback function to generate the
sourcearray dynamically, but that’s simple. Have a look at the docs: