HTML
<body>
<header>
<div class="container-fluid">
<h1>Wikipedia Viewer</h1>
<form id="userInput">
<input id="query" type="text" name="userInput" value="" placeholder="Search">
<input id="submitBtn" type="submit" name="send" value="Click!">
</form>
<a id="randomArticle" href="https://en.wikipedia.org/wiki/Special:Random"><i class="fa fa-random fa-2x" aria-hidden="true"></i></a>
</div>
</header>
<div class="results">
</div>
</body>
</html>
Javascript
var url = "https://en.wikipedia.org/w/api.php?action=opensearch&datatype=json&callback=?&search=";
var userQuery = "";
var html = "";
$(document).ready(function () {
$("#userInput").submit(function (e) {
userQuery = document.getElementById("query").value;
$.getJSON(url + userQuery, function (data) {
for (var i = 0; i < data[1].length; i++) {
html = '<article><a href="' + data[3][i] + '"target="_blank"><h3>' + data[1][i] + '</h3><p>' + data[2][i] + '</p></a></article>';
$(".results").append(html);
}
});
});
});
When I get to the .getJSON it just won’t succeed, I have tested the URL and the JSON comes up in browser but the request using Jquery just won’t work.
All you need to do is to add a jQuery
preventDefault()
Method to your button click event as in: