-
Notifications
You must be signed in to change notification settings - Fork 0
/
script_for_index.js
56 lines (46 loc) · 2.02 KB
/
script_for_index.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
document.addEventListener("DOMContentLoaded", function () {
const cardContent = document.querySelector(".card-content");
fetch("https://panorbit.in/api/users.json")
.then((response) => response.json())
.then((data) => {
const users = data.users;
const table = createTable(users);
cardContent.appendChild(table);
})
.catch((error) => {
console.error("Error fetching user data:", error);
});
function createTable(users) {
const table = document.createElement("table");
table.classList.add("user-table");
const headerRow = document.createElement("tr");
headerRow.innerHTML = "<th>User</th><th>Name</th>";
table.appendChild(headerRow);
users.forEach((user) => {
const row = document.createElement("tr");
const userCell = document.createElement("td");
const img = document.createElement("img");
img.src = user.profilepicture;
img.alt = user.name;
img.classList.add("user-icon");
userCell.appendChild(img);
row.appendChild(userCell);
const nameCell = document.createElement("td");
nameCell.textContent = user.name;
nameCell.classList.add("user-name"); // Add a class to identify the user names
row.appendChild(nameCell);
table.appendChild(row);
});
return table;
}
// Attach click event listener to each user name
cardContent.addEventListener("click", function (event) {
// Check if the clicked element is a user name
if (event.target.classList.contains("user-name")) {
// Get the name of the user
const userName = event.target.textContent;
// Open the profile home page with the user's name as a query parameter
window.open(`profile.html?name=${encodeURIComponent(userName)}`, "_blank");
}
});
});