Skip to content

Instantly share code, notes, and snippets.

@tyrauber
Created August 6, 2012 02:46
Show Gist options
  • Save tyrauber/3269339 to your computer and use it in GitHub Desktop.
Save tyrauber/3269339 to your computer and use it in GitHub Desktop.
autocomplete snippet
function submitForm(){
$.ajax({
type: 'POST',
url: "admin",
data: {
utf8: "✓",
authenticity_token: $("meta[name='csrf-token']").attr('content'),
search: {
q: $('#search_q').val()
},
commit: 'Search'
}
});
}
$.ajaxSetup({
headers: {
"X-CSRF-Token": $("meta[name='csrf-token']").attr('content')
}
});
$( "#search_q" ).autocomplete({
source: function( request, response ) {
$.ajax({
url: "/api/autocomplete.json",
dataType: "json",
data: {
q: request.term },
success: function( data ) {
response( $.map( data.features, function( item ) {
return {
label: item.name,
value: item.name
}
}))}
});
},
minLength: 2,
select: function( event, ui ) {
if($('#move_center_my_location').checked){var center = 'my_location'}else{center ='search_results'}
$.ajax({
type: 'POST',
url: "admin",
data: {
utf8: "✓",
authenticity_token: $("meta[name='csrf-token']").attr('content'),
search: {
q: ui.item.label
},
commit: 'Search'
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment