-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnews.js
38 lines (32 loc) · 1.57 KB
/
news.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
let newsAccordion = document.getElementById('newsAccordion');
const xhr = new XMLHttpRequest();
xhr.open('GET' ,'https://gnews.io/api/v4/top-headlines?sources=the-times-of-india&token=1d7bd17f2b448bd0b48884a54d32c899');
xhr.onload = function () {
if (this.status === 200) {
let json = JSON.parse(this.responseText);
let articles = json.articles;
console.log(articles);
let newsHtml = "";
articles.forEach(function(element, index) {
let news = `<div class="card">
<div class="card-header" id="heading${index}">
<h2 class="mb-0">
<button class="btn btn-link collapsed" type="button" data-toggle="collapse" data-target="#collapse${index}"
aria-expanded="false" aria-controls="collapse${index}">
<b>Breaking News ${index+1}:</b> ${element["title"]}
</button>
</h2>
</div>
<div id="collapse${index}" class="collapse" aria-labelledby="heading${index}" data-parent="#newsAccordion">
<div class="card-body"> ${element["content"]}. <a href="${element['url']}" target="_blank" >Read more here</a> </div>
</div>
</div>`;
newsHtml += news;
});
newsAccordion.innerHTML = newsHtml;
}
else {
console.log("Some error occured")
}
}
xhr.send()