Created
April 3, 2014 13:27
-
-
Save FifthSurprise/9954300 to your computer and use it in GitHub Desktop.
Javascript Autocomplete sample
This file contains 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
function updateSearchValues() { | |
function collect(arr, value) { | |
var results = []; | |
for (var i = 0; i < arr.length; i++) { | |
results.push(arr[i][value]); | |
} | |
return results; | |
}; | |
var booksearch = $("#booksearch"); | |
//Only request if 3 or more characters entered into search | |
if (booksearch.val().length >= 3) { | |
var availableTags = $.getJSON("/books.json?", { | |
query: booksearch.val(), | |
format: "JSON", | |
}).success(function(data) { | |
var titles = $.unique(collect(data, | |
"title")); | |
booksearch.autocomplete({ | |
source: titles, | |
select: function(event, ui) { | |
window.location.href = "/books?utf8=%E2%9C%93&query=" + ui.item.value + "&commit=search"; | |
} | |
}); | |
}); | |
} | |
}; | |
$(document).ready(function() { | |
$("#booksearch").bind('input', function() { | |
updateSearchValues(); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment