-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.js
37 lines (30 loc) · 1.12 KB
/
fetch.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
const $ = document.querySelector.bind(document);
let counter = 0
const url = "https://reqres.in/api/users";
// getting the data from the server
async function getData(url) {
const response = await fetch(url);
if (response.status === 200) {
const data = await response.json()
return data
} else {
throw new Error("your data is not found")
}
}
// filling the html elements with the recieved data
getData(url).then(function (data) {
data.data.forEach(item => {
const src = (data.data[counter]["avatar"]);
$(`img[id='${counter+1}']`).setAttribute("src", `${src}`)
const id = (data.data[counter]["id"]);
$(`span[id = 'userid${counter+1}']`).textContent = id
const email = (data.data[counter]["email"]);
$(`span[id = 'useremail${counter+1}']`).textContent = email
const firstName = (data.data[counter]["first_name"]);
const lastName = (data.data[counter]["last_name"]);
$(`span[id = 'username${counter+1}']`).textContent = firstName +" "+ lastName
counter++
});
}).catch(function (error) {
alert(error)
})