-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
41 lines (33 loc) · 1.03 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
const usernameInput = document.getElementById("username");
const searchButton = document.getElementById("search");
const profile = document.getElementById('profile');
const error = document.getElementById('error');
usernameInput.addEventListener('keypress', (e) => {
if (e.key == 'Enter') {
ftch();
}
})
searchButton.addEventListener('click', () => {
ftch();
});
async function ftch() {
const username = usernameInput.value;
if (!username) {
error.innerText = "Please enter a valid username";
return;
}
try {
const response = await fetch(`https://api.github.com/users/${username}`);
const data = await response.json();
profile.innerHTML = `
<img src="${data.avatar_url}" alt="${data.name}">
<h2>${data.name}</h2>
<p>Bio: ${data.bio}</p>
<p>GitHub URL: <a href="${data.html_url}">${data.html_url}</a></p>
`;
}
catch (err) {
console.log(err);
error.innerText = "Error fetching the data from GIthub api"
}
}