-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
45 lines (45 loc) · 1.48 KB
/
script.js
File metadata and controls
45 lines (45 loc) · 1.48 KB
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
let a = ["biryani","burger","butter-chicken","dessert","dosa","idly","pasta","pizza","rice","samosa"];
//For dropdown box
let dropdown = document.querySelector(".item");
for (const i of a){
let opt = document.createElement("option");
opt.value = i;
opt.innerText = i;
dropdown.prepend(opt);
}
//For Submit button
let btn = document.querySelector(".submit");
btn.addEventListener("click",()=>{
const selected = dropdown.value;
let img = document.querySelectorAll("img");
for(const i of img){
imgLoader(`https://foodish-api.com/api/images/${selected}`,i);
}
});
//For Random Button
let random = document.querySelector(".random");
random.addEventListener("click",()=>{
let idx = Math.floor(Math.random() * (9));
let img = document.querySelectorAll("img");
for(const i of img){
imgLoader(`https://foodish-api.com/api/images/${a[idx]}`,i);
}
});
//Adding images to imgCont
let cont = document.querySelector(".imgCont");
for(let i = 0;i<16;i++){
let el = document.createElement("img");
cont.prepend(el);
}
//For collecting all the img elements
let img = document.querySelectorAll("img");
for(const i of img){
imgLoader("https://foodish-api.com/api",i);
}
//A function to set the img src if api call is successfull(or print the error in the console in case of failure)
async function imgLoader(url, i) {
let response =await fetch(url);
let data = await response.json();
i.src = data.image;
response.catch(err=>{console.log(err)});
}