Skip to content

Instantly share code, notes, and snippets.

@schowdhury
Created January 27, 2012 20:19
Show Gist options
  • Save schowdhury/1690705 to your computer and use it in GitHub Desktop.
Save schowdhury/1690705 to your computer and use it in GitHub Desktop.
jquery autocomplete return id instead of text
var select_function = function(event, ui) {
if ($(this).data("event-name") == undefined) {
$(this).data("event-name", $(this).attr("name"));
}
if(ui.item) {
var hidden_location;
// currently only care if there's a child, not checking the childs type
if ($(this).children().size() == 0 ) {
hidden_location = $('<input type=hidden></input>')
hidden_location.attr("name", $(this).data("event-name"))
$(this).append(hidden_location);
} else {
hidden_location = $(this).children();
}
hidden_location.attr("value", ui.item.id);
$(this).attr("name","");
}
}
$(function() {
setAutoComplete($("#my-textfield-that-needs-autocomplete-id"));
});
function setAutoComplete(element) {
element.autocomplete({
source: "<%= some url...%>",
minLength: 3,
select: select_function
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment