Created
January 3, 2013 05:17
-
-
Save chandsie/4441018 to your computer and use it in GitHub Desktop.
YouTube Search Autocomplete Example
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
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