Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't get $(this) working in jQueryUI autocomplete

I'm trying to create a generic autocomplete script using jQueryUI. The autocomplete should work for every:

<input type='text' class='autocomplete' id='foo'/> <input type='text' class='autocomplete' id='bar'/> ... 

Now I'm trying to access 'foo' or 'bar' in the source function using $(this), but when alerting I always get 'undefined'.

$('input.autocomplete').autocomplete({     source: function(req, add){         var id = $(this).attr('id');         alert(id);     } }); 

What am I doing wrong?

like image 847
bart Avatar asked Nov 28 '10 20:11

bart


People also ask

Why is jQuery autocomplete not working?

autocomplete is not a function" jQuery error occurs for multiple reasons: Forgetting to include the jQuery UI library. Loading the jQuery UI library before the jQuery library. Loading the jQuery library twice.

Why autocomplete is not working?

If the autocomplete feature is enabled but still not working, try disabling the account sync feature in the You and Google tab as mentioned previously. Click on Turn off to the right of your name and email address. Then restart Google Chrome and enable sync again.

What is jQuery autocomplete?

Advertisements. Auto completion is a mechanism frequently used in modern websites to provide the user with a list of suggestions for the beginning of the word, which he/she has typed in a text box. The user can then select an item from the list, which will be displayed in the input field.

What is autocomplete in JavaScript?

The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try. The datasource is a simple JavaScript array, provided to the widget using the source-option. 1.


1 Answers

Setup autocomplete separately for each item in your selection, using a closure to hold a reference to the relevant element. Something like the following:

$('input.autocomplete').each(function(i, el) {     el = $(el);     el.autocomplete({         source: function(req, add) {             var id = el.attr('id');             alert(id);         }     }); }); 

Alternative (edit)

I don't see why there is such resistance to using each(): it works, the code is very clear and readable, and it introduces no issues with efficiency; but if you're determined to avoid each(), here's an alternative...

*PLEASE NOTE: the following approach relies (a little bit) on the internals of jQuery Autocomplete, so I'd recommend the first option... but the choice is yours.

$('input.autocomplete').autocomplete({         source: function(req, add) {             var id = this.element.attr('id');             alert(id);         }     }); }); 

That will work, at least until/unless they change the way the source() function is called from within the autocomplete plugin.

So, you have two options... something for everyone.

like image 87
Lee Avatar answered Oct 26 '22 03:10

Lee