Created
January 27, 2012 20:19
-
-
Save schowdhury/1690705 to your computer and use it in GitHub Desktop.
jquery autocomplete return id instead of text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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