Find jQuery Autocomplete Input ID

The other day I was working with jQuery autocomplete where the autocomplete was being applied to a collection of input elements.  One of the issues I was running into was that I needed a reference to the input that the automplete was extending during the execution of the select event.  The way to do this is to store a reference to the input inside the function executing the select event, in essence create a closer.  Below shows how this done, the call to $(this) resolves to the input that is being autocompleted.

$(".all-my-autos")
.autocomplete({
source: "auto-me.php",
minLength: 2,
delay: 500,
select: function (event, ui) {
var input = $(this); // reference to input
// do what you need with input...
alert(input.attr('id'));
}
});

1 comment:

The Saint said...

This was a time saver. Thanks!