-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjscript.js
34 lines (32 loc) · 946 Bytes
/
jscript.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
$(document).ready(function(){
$('#search').click(function(){
//getting searchterm
var searchTerm = $('#searchTerm').val();
//API url
var url = "https://en.wikipedia.org/w/api.php?action=opensearch&search="+searchTerm+"&format=json&callback=?";
$.ajax({
type: 'GET',
url: url,
async: false,
dataType: "json",
success: function(data){
// console.log(data[1][0]);
// console.log(data[2][0]);
// console.log(data[3][0]);
$('#output').html('');
for(var i=0;i<data[1].length;i++){
$('#output').prepend("<li><a href="+data[3][i]+"><h2>"+ data[1][i]+"</h2></a><p>"+data[2][i]+"</p></li>");
}
$('#searchTerm').val('');
},
error: function(errorMessage){
alert("ERROR");
}
})
});
$('#searchTerm').keypress(function(e){
if(e.which ==13){
$('#search').click();
}
});
});