If I understood correctly, your problem looks very similar to one I saw in another topic. I’ll adapt my answer to your case, let’s see if it solves your problem:
$(document).on("focus",[selector],function(e) {
if ( !$(this).data("autocomplete") ) { // If the autocomplete wasn't called yet:
$(this).autocomplete({ // call it
source:['abc','ade','afg'] // passing some parameters
});
}
});
Working example at jsFiddle. I used focus
instead of keydown
because I need to re-trigger the event, and I don’t know how to do it with key/mouse events.
Update: You could also consider using clone
, if your additional inputs will have the same autocomplete as an existing one:
var count = 0;
$(cloneButton).click(function() {
var newid = "cloned_" + (count++); // Generates a unique id
var clone = $(source) // the input with autocomplete your want to clone
.clone()
.attr("id",newid) // Must give a new id with clone
.val("") // Clear the value (optional)
.appendTo(target);
});
See the updated fiddle. There are also jQuery template plugins that might make this solution easier (although I haven’t used one myself yet, so I can’t talk from experience).