jQuery autocomplete and event concept
The jquery website gave the autocomplete code I needed, which specifically is autocomplete for a comma separated list of words. But when I use the autocomplete code something does not work. I am building a single page application where divs are being swapped in and out.
When I reach this function, I confirmed through chrome dev tools that my array of possible words has been generated after consuming a web service I built. I must be missing something conceptually since I don't see anything wrong. 'allNames' is the autocomplete source array. 'myTextField' is the id of my input text field. I have a click listener whose handler is autocomplete, which uses the helper functions split and extractVals. Here's my code snippet:
- allNames.sort();
- /* get all tokens, delimited by ", " */
- function split( val ) {
- return val.split( /,\s*/ );
- }
-
- function extractLast( term ) {
- return split( term ).pop();
- }
-
- // start autocomplete multiple vals
- $( "#myTextField" )
- .autocomplete(
- {
- minLength: 0,
- source: function( request, response ) {
- response( $.ui.autocomplete.filter(allNames,
- extractLast(request.term))
- );
- },
- focus: function() {
- return false;
- },
- select: function( event, ui ) {
- // terms is an array of tokens
- var terms = split( this.value );
- terms.pop();
- terms.push(ui.item.value);
- terms.push( "" );
- this.value = terms.join(", ");
- return false;
- }
- }
- );
- /* show this div in my single page app container identified by "ReplaceMe". "htmlContent" so far is an html string containing the textfield identified by 'myTextField' */
- $("#ReplaceMe").html(htmlContent);