I am trying to get article from Wikipedia base on user's input. But the code is not working and I can't figure out the error(s) in my code.
var wResult = document.getElementById("result");
var summary, articleTitle, apiUrl;
function getResult(){
var inputTitle = document.getElementById("title").value;
apiUrl = "https://en.wikipedia.org/w/api.php?action=query&format=json&list=search&indexpageids=1&redirects=1&formatversion=latest&srsearch="+inputTitle+"&srlimit=10";
$.getJSON(apiUrl, function(response){
summary = response.query.search[0].snippet;
wResult.innerHTML = summary;
});
}
var inputBtn = document.getElementById("btn");
inputBtn.addEventListener("click", getResult);
Here is the HTML code:
<body>
<h1 class="text-center"> Wikipedia Viewer </h1>
<form class="col-lg-4 input-group" name="search" id="form">
<input type="text" id="title" placeholder="Search title" class="form-control">
<span class="input-group-btn">
<button type="button" class="btn btn-secondary" id="btn">Go!</button>
</span>
</form>
<div id="result">
</div>
</body>
You can check it here.