A Pen by Maureen Hernandez on CodePen.
Created
November 22, 2016 02:51
-
-
Save MauHernandez/32523baf2ec8cfefbb14b5e4b8183150 to your computer and use it in GitHub Desktop.
FreeCodeCamp - Build Wikipedia Viewer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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 | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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%; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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