Created
March 31, 2024 09:54
-
-
Save QuocCao-dev/94cc513c818e6f34e24d4ab4212e51a8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name, email, phone, location, age, picture | |
// Events listener: click (generate user) | |
const btnTag = document.querySelector("#generate"); | |
function fetchUser() { | |
fetch("https://randomuser.me/api") | |
.then((response) => response.json()) | |
.then((data) => { | |
const randomUser = data.results[0]; | |
displayUser(randomUser); | |
}); | |
} | |
function displayUser(user) { | |
const userDOM = document.querySelector("#user"); | |
userDOM.innerHTML = ` | |
<div class="flex justify-between"> | |
<div class="flex"> | |
<img | |
class="w-48 h-48 rounded-full mr-8" | |
src="${user.picture.large}" | |
/> | |
<div class="space-y-3"> | |
<p class="text-xl"> | |
<span class="font-bold">Name: </span>${user.name.first} ${user.name.last} | |
</p> | |
<p class="text-xl"> | |
<span class="font-bold">Email: </span> ${user.email} | |
</p> | |
<p class="text-xl"> | |
<span class="font-bold">Phone: </span> ${user.phone} | |
</p> | |
<p class="text-xl"> | |
<span class="font-bold">Location: </span> ${user.location.city} ${user.location.country} | |
</p> | |
<p class="text-xl"><span class="font-bold">Age: </span> ${user.dob.age}</p> | |
</div> | |
</div> | |
</div> | |
`; | |
} | |
fetchUser(); | |
btnTag.addEventListener("click", () => { | |
fetchUser(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment