Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sribalakumar/1e8ead00972ec737b5d5 to your computer and use it in GitHub Desktop.
Save sribalakumar/1e8ead00972ec737b5d5 to your computer and use it in GitHub Desktop.
jQuery Auto Complete consuming Google Suggestion
<script src="https://code.jquery.com/jquery-1.11.0.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">
<script>
var google_suggest_url = "http://suggestqueries.google.com/complete/search?client=chrome&q=";
$(function() {
$("#google-suggest").autocomplete({
source: function(request, response) {
var temp_arr = request.term.split(/[.,]+/); // Regex so that sentences after , or . are treated as new sentence.
var search_sentence = temp_arr[temp_arr.length-1];
$.ajax({
url: google_suggest_url+search_sentence,
dataType: "jsonp",
success: function(data) {
response($.map(data[1], function(v,i) {
return { value: v };
}));
},
error: function( data ) {
console.log("ERROR");
console.log(data);
}
});
}
});
});
</script>
<div class="ui-widget">
<label for="tags">Google Auto Suggest: </label>
<input type="text" id="google-suggest" size="50"/>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment