Reputation: 315
I am trying to redirect the page on the selection of an item on autocomplete. I am using JSON to populate the autocomplete. How can I get it to redirect to a URL with the ID from the JSON? I tried using the select function but that doesn't seem to work. It is not even going to a URL when selecting an item. Any help would be greatly appreciated!
<script type="text/javascript">
$(function() {
$('#user').autocomplete({
minLength: 2,
source: '<%= admin_users_path(:json) %>',
focus: function(event, ui) {
$('#user').val(ui.item.first_name + " " + ui.item.last_name);
return false;
},
select: function(event, ui) {
location.href="/admin/users/" + val(ui.item.id) + "/edit";
}
})
.data( "ui-autocomplete" )._renderItem = function( ul, item ) {
return $( "<li>" )
.data( "ui-autocomplete-item", item )
.append( "<a>" + item.first_name + " " + item.last_name + "</a>" )
.appendTo( ul );
};
});
</script>
Upvotes: 2
Views: 5702
Reputation: 315
If I change this, it works.
select: function(event, ui) {
location.href="/admin/users/" + ui.item.id + "/edit";
}
Upvotes: 4