Created
October 1, 2014 07:56
-
-
Save sribalakumar/1e8ead00972ec737b5d5 to your computer and use it in GitHub Desktop.
jQuery Auto Complete consuming Google Suggestion
This file contains hidden or 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
<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