-
Notifications
You must be signed in to change notification settings - Fork 0
/
apple.js
34 lines (32 loc) · 990 Bytes
/
apple.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
let getData = async () => {
try {
let res = await fetch(
"https://newsapi.org/v2/everything?q=apple&from=2022-08-24&to=2022-08-24&sortBy=popularity&apiKey=dcfb7e46f19f4785a64dea9422ea2784"
);
let data = await res.json();
console.log(data);
appendData(data.articles);
} catch (e) {
console.log(e);
}
};
getData();
let array = JSON.parse(localStorage.getItem("news")) || [];
function appendData(data) {
let container = document.getElementById("kd_mainHeading");
data.forEach((el) => {
// console.log('el:', el)
let div = document.createElement("div");
div.addEventListener("click", () => {
array.push(el);
localStorage.setItem("news", JSON.stringify(array));
window.location.href = "./show.html";
});
let img = document.createElement("img");
img.src = el.urlToImage;
let Title = document.createElement("h4");
Title.innerText = el.title;
div.append(img, Title);
container.append(div);
});
}