Skip to content

Instantly share code, notes, and snippets.

@napoler
Forked from chandsie/search.js
Created September 20, 2016 08:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save napoler/2fbf801b68c7c8bf5aa521b28d2af542 to your computer and use it in GitHub Desktop.
Save napoler/2fbf801b68c7c8bf5aa521b28d2af542 to your computer and use it in GitHub Desktop.
YouTube Search Autocomplete Example
var suggestCallBack; // global var for autocomplete jsonp
$(document).ready(function () {
$("#search").autocomplete({
source: function(request, response) {
$.getJSON("http://suggestqueries.google.com/complete/search?callback=?",
{
"hl":"en", // Language
"ds":"yt", // Restrict lookup to youtube
"jsonp":"suggestCallBack", // jsonp callback function name
"q":request.term, // query term
"client":"youtube" // force youtube style response, i.e. jsonp
}
);
suggestCallBack = function (data) {
var suggestions = [];
$.each(data[1], function(key, val) {
suggestions.push({"value":val[0]});
});
suggestions.length = 5; // prune suggestions list to only 5 items
response(suggestions);
};
},
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment