-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
52 lines (44 loc) · 1.79 KB
/
script.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const searchtext = document.getElementById("tx");
async function fetchNews(category) {
try {
const url = `https://saurav.tech/NewsAPI/top-headlines/category/${category}/in.json`;
const response = await fetch(url);
const data = await response.json();
const newsList = document.getElementById('news-list');
newsList.innerHTML = '';
if (data.articles) {
data.articles.forEach(article => {
const articleElement = document.createElement('div');
articleElement.classList.add('article');
articleElement.innerHTML = `
<h2>${article.title}</h2>
<p>${article.description}</p>
<img src="${article.urlToImage}" alt="${article.title}">
<a href="${article.url}" target="_blank">Read more</a>
`;
newsList.appendChild(articleElement);
});
} else {
console.error('Error fetching news:', data.message || 'Unknown error');
}
} catch (error) {
console.error('Error fetching news:', error);
}
}
document.addEventListener('DOMContentLoaded', function () {
fetchNews('general');
const navLinks = document.querySelectorAll('nav a');
navLinks.forEach(link => {
link.addEventListener('click', function (event) {
event.preventDefault();
const category = this.getAttribute('href').substring(1);
fetchNews(category);
});
});
const searchButton = document.getElementById("btn");
searchButton.addEventListener("click", () => {
const category = searchtext.value;
if (!category) return;
fetchNews(category);
});
});