I made use of fetch API(A browser based request API, LOL). You can replace with any of your choice :)
Last active
April 13, 2017 16:49
-
-
Save ooade/9a4c83858bad9a21098ea11bec7bd88d to your computer and use it in GitHub Desktop.
Grab posts from subreddit
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
.sub { | |
background-color: #fff; | |
font-size: 14px; | |
padding: 10px; | |
border-bottom: 1px solid #ddd; | |
} |
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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<meta name="viewport" content="width=device-width"> | |
<title>JS Bin</title> | |
<link rel='stylesheet' href='index.css'/> | |
</head> | |
<body> | |
<div id='post'></div> | |
<script src='index.js'></script> | |
</body> | |
</html> |
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
const post = document.getElementById('post'); | |
fetch('https://www.reddit.com/r/EPL/.json') | |
.then(res => res.json()) // Fetch JSON | |
.then(res => res.data.children) // Grab the children | |
.then(res => { | |
res.map((sub, n) => { // Loop through each Subreddits | |
let { data: { id, title } } = sub; | |
// Let's render our title to the page with numbers... | |
// 1.) blah blah blah | |
let textnode = document.createTextNode(`${n + 1}.) ${title}`); | |
let div = document.createElement('div'); | |
div.id = id; | |
div.className= 'sub'; | |
div.appendChild(textnode); | |
post.appendChild(div) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment