-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
57 lines (51 loc) · 1.93 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
53
54
55
56
57
const apikey="eac2fcf0ec7f4b3498203e08eeaf32cf";
const url="https://newsapi.org/v2/everything?q=";
window.addEventListener('load',()=>fetchData("India"));
async function fetchData(topic){
const ans = await fetch(`${url}${topic}&apiKey=${apikey}`);
const data=await ans.json();
bindData(data.articles);
}
function bindData(arti){
const cardscontainer=document.getElementById('cards-container');
const templatecards=document.getElementById('template-cards');
cardscontainer.innerHTML="";
arti.forEach(arti => {
if(!arti.urlToImage)return;
const cardclone=templatecards.content.cloneNode(true);//copying the contents of template and storing it in var
datafill(cardclone,arti);
cardscontainer.appendChild(cardclone);
});
}
function datafill(cardclone,arti){
const newsimg=cardclone.querySelector('#newsimg');
const newstit=cardclone.querySelector('#news-title');
const newsdet=cardclone.querySelector('#newsdet');
const newspara=cardclone.querySelector('#content-para');
newsimg.src=arti.urlToImage;
newstit.innerHTML=arti.title;
newsdet.innerHTML=arti.description;
newspara.innerHTML=arti.content;
cardclone.firstElementChild.addEventListener('click',()=>{
window.open(arti.url,"_blank");
})
}
// const navlinks=document.getElementsByClassName('hover-links');
// Array.from(navlinks).forEach((links)=>{addEventListener('click',(topic)=>{
// const query=links.innerHTML;
// fetchData(query);
// });
// });
function navclick(newstopic){
fetchData(newstopic);
}
const searchbtn=document.getElementById('searchbtn');
const searchtext=document.getElementById('searchbar');
searchbtn.addEventListener('click',()=>{
const val=searchtext.value.trim();
if(!val)return;
else fetchData(val);
});
function reload(){
window.location.reload();
}