Skip to content

Instantly share code, notes, and snippets.

@akhil-gautam
Created May 9, 2021 12:40
Show Gist options
  • Save akhil-gautam/efc227b23b31e18daefde39cd4e893dd to your computer and use it in GitHub Desktop.
Save akhil-gautam/efc227b23b31e18daefde39cd4e893dd to your computer and use it in GitHub Desktop.
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