JSFiddle - React, Tailwind, and code Playground
HTML
<input type=text id="query" value="sher" />
<input type="button" id="btn" value="search" />
<pre id="response"></pre>
CSS
/*
* Safe to ignore:
* This is just to make the JSON look good
*
* CREDIT:
* http://stackoverflow.com/a/7220510/123415
*/
pre {
outline: 1px solid #ccc;
padding: 5px;
margin: 5px;
}
.string {
color: green;
}
.number {
color: darkorange;
}
.boolean {
color: blue;
}
.null {
color: red;
}
.key {
color: #008;
}
JavaScript
var requestUrl = "https://suggestqueries.google.com/complete/search?client=chrome&ds=bo&q="
$(document).on("click", "#btn", function () {
var queryTerm = $("#query").val();
$.ajax({
url: requestUrl + queryTerm,
dataType: "jsonp",
success: function (response) {
$("#response").html(syntaxHighlight(response));
}
});
});
/*
* --------- YOU ONLY NEED WHAT IS ABOVE THIS LINE ---------
*/
//Just for fun... credit: http://stackoverflow.com/a/7220510/123415
function syntaxHighlight(json) {
if (typeof json != 'string') {
json = JSON.stringify(json, undefined, 2);
}
json = json.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
return json.replace(/("(\\u[a-zA-Z0-9]{4}|\\[^u]|[^\\"])*"(\s*:)?|\b(true|false|null)\b|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?)/g, function (match) {
var cls = 'number';
if (/^"/.test(match)) {
if (/:$/.test(match)) {
cls = 'key';
} else {
cls = 'string';
}
} else if (/true|false/.test(match)) {
cls = 'boolean';
} else if (/null/.test(match)) {
cls = 'null';
}
return '<span class="' + cls + '">' + match + '</span>';
});
}