Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MauHernandez/32523baf2ec8cfefbb14b5e4b8183150 to your computer and use it in GitHub Desktop.
Save MauHernandez/32523baf2ec8cfefbb14b5e4b8183150 to your computer and use it in GitHub Desktop.
FreeCodeCamp - Build Wikipedia Viewer
<div class="container-fluid">
<a href="https://en.wikipedia.org/wiki/Special:Random" target="_blank">
<button class="btn btn-block"> Get a random result</button>
</a>
<div class="search">
<form class="form-inline">
<input type="text" class="form-control" id="input" placeholder="I'm batman"/>
<button class="btn btn-primary" id="search" type="submit">Search</button>
</form>
</div>
<div id="results"> </div>
<div class="well">Please note this page uses wikipedia API so the results are based on real data</div>
</div>
$(document).ready(function(){
validate();
function validate(){
if ($("#input").val().length > 0 ){
$("#search").prop("disabled", false);
}else{
$("#search").prop("disabled", true);
}
}
$("#input").change(validate);
$("#search").on("click", function(event){
var textForSearch = encodeURIComponent($("#input").val());
var apiURL = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&exlimit=max&format=json&exsentences=1&exintro=&explaintext=&generator=search&gsrlimit=10&gsrsearch='+textForSearch+'&callback=?'
$.getJSON(apiURL, function(json){
var htmlStr = '';
var result = json.query.pages;
var pages = Object.keys(result);
pages.forEach(function(article){
htmlStr +='<div class="col-xs-12 page-result">';
var link = result[article].pageid
htmlStr += '<a href="'+'https://en.wikipedia.org/?curid='+link+'">';
var title = result[article].title;
htmlStr += '<h3>'+ title +'</h3>';
var content = result[article].extract;
htmlStr += '<p>'+ content +'</p>';
htmlStr += '</a></div>';
});
$("#results").html(htmlStr);
}); //getJson
$(".well").remove();
event.preventDefault();
}); //onClick
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
#results{
clear: both;
overflow: hidden;
}
.search{
padding: 20px 10px;
}
.page-result{
border-radius: 15px;
border: solid 3px white;
background-color: #f7f7f7;
}
.well{
text-align:center;
position: absolute;
bottom: 0;
width:95%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment