Created
May 9, 2021 12:40
-
-
Save akhil-gautam/efc227b23b31e18daefde39cd4e893dd to your computer and use it in GitHub Desktop.
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 API_URL = | |
'https://newsapi.org/v2/top-headlines?country=us&apiKey=65fe12450a1942bab3176e10e8c48062'; | |
const getParentEl = () => document.querySelector('.list'); | |
const getNews = () => { | |
fetch(API_URL) | |
.then((res) => res.json()) | |
.then((res) => { | |
createList(res.articles); | |
}); | |
}; | |
const createListItem = (article) => { | |
const newsDiv = document.createElement('div'); | |
newsDiv.setAttribute('class', 'newsDiv'); | |
const imgEL = document.createElement('img'); | |
imgEL.setAttribute('src', article.urlToImage); | |
imgEL.setAttribute('alt', article.title); | |
imgEL.setAttribute('class', 'newsBanner'); | |
const titleEl = document.createElement('p'); | |
titleEl.innerText = article.title; | |
titleEl.setAttribute('class', 'titleEl'); | |
newsDiv.appendChild(imgEL); | |
newsDiv.appendChild(titleEl); | |
getParentEl().appendChild(newsDiv); | |
}; | |
const createList = (articles) => { | |
articles.forEach((article) => { | |
createListItem(article); | |
}); | |
}; | |
getNews(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment