views:

1676

answers:

4

I have a form with a variable number of autocomplete fields that use the same select list. These fields are added and removed as needed. Whenever a parameter on the page is changed, I (try to) update the list for all the fields by first calling unbind() then autocomplete() with a new parameter added to the url.

$('input.foo').unbind().autocomplete(url?new_param=bar);

The problem is that unbind() does not seem to be unbinding. When I type in the input field, it fires off the entire history of autocomplete events.

I also tried flushCache to no avail.

How do I clear out the old events?

Thanks.

+1  A: 

The autocomplete plugin adds a function called unautocomplete() to remove autocomplete() from fields that have it, so it should work if change your code to:

$('input.foo').unautocomplete().autocomplete('url?new_param=bar');

This unautocomplete() function should remove all of the old events.

Deeksy
Hey, thanks for responding Deeksy. I tried unautocomplete, but it didn't work. I did, however use removeData('events') and that worked!$('input.foo').removeData('events').autocomplete('url?new_param=bar');Note that if you do that, any other events attached to that element will get blown away.
cinematic
@cinematic Yeah, looking into the autocomplete plugin code, it's unfortunate that they namespace the 'keydown' event, but not the click, focus or other events that are added to the input as part of the autocomplete. If they'd bothered to follow through, it would just be a matter of going .unbind('keypress.autocomplete').unbind('click.autocomplete'). etc. though that should all be in the unautocomplete() I suppose.
Deeksy
Nice... Thank you for solution.
Cesar
This solution causes items to be skipped over on down key in IE when it is reloaded.
Yuriy Faktorovich
+1  A: 

Try this

$ ('input.foo').unbind('.autocomplete').autocomplete ('url?new_param=bar') ;

Rubasu
+1  A: 

I think your problem is that you don't have a form enclosing the autocomplete input field. If you look in the code of jquery.autocomplete.js it does $(input.form).unbind(".autocomplete"); on unautocomplete(), which means the input must live inside a form tag in order to work.

A: 

jQuery provides flushCache() method (http://docs.jquery.com/Plugins/Autocomplete) to remove the contents already matched through auto-complete.

For example, if the html element used is <input type="text" id="txtElement"/> and auto-complete is associated with it. The flushCache can be given as follows: $("#txtElement").flushCache(); and this would solve the problem.

learningloop