Skip to content

Instantly share code, notes, and snippets.

@bomboclat
Last active April 18, 2018 15:04
Show Gist options
  • Save bomboclat/b2c8e05fc6f09e05d4399ef661d8943a to your computer and use it in GitHub Desktop.
Save bomboclat/b2c8e05fc6f09e05d4399ef661d8943a to your computer and use it in GitHub Desktop.
$(document).ready(function() {
$('select').formSelect();
$("#action").on("click", search);
$("#search").keypress(function(e) {
if(e.which == 13) {
$("#action").click();
}
});
});
function search() {
$("#random").hide();
var searchString = $("#search").val();
var language = $("#dropdown").val();
var url = null;
if (language==2) {
url = "https://en.wikipedia.org/w/api.php"
} else if (language==1) {
url = "https://it.wikipedia.org/w/api.php"
}
$.ajax({
url: url,
data: {
origin: "*",
action: "opensearch",
format: "json",
headers: { 'Api-User-Agent': 'Googlepedia exercise 1.0' },
formatversion: "2",
search: searchString,
},
success: function (result) {
$('#search-results').empty();
for (i=0; i<10; i++) {
var title = result[1][i];
var description = result[2][i];
var url = result[3][i];
$('#search-results').append('<div style="cursor: pointer;" class="card hoverable z-depth-2"><div'+ ' id='+[i]+' onclick='+'"window.location='+"'"+url+"'"+';"'+' class="card-content">'+'<span class="card-title">'+'<b>'+title+'</b>'+'</span>'+'<p>'+description+'</p>'+'</div></div>');
};
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment