|
| 1 | +const result = document.getElementById("result"); |
| 2 | +const filter = document.getElementById("filter"); |
| 3 | +const listItems = []; |
| 4 | + |
| 5 | +getData(); |
| 6 | + |
| 7 | +filter.addEventListener("input", (e) => filterData(e.target.value)); |
| 8 | + |
| 9 | +async function getData() { |
| 10 | + const res = await fetch("https://randomuser.me/api?results=50"); |
| 11 | + const { results } = await res.json(); |
| 12 | + |
| 13 | + result.innerHTML = ""; |
| 14 | + |
| 15 | + results.forEach((user) => { |
| 16 | + const li = document.createElement("li"); |
| 17 | + listItems.push(li); |
| 18 | + |
| 19 | + li.innerHTML = ` |
| 20 | + <img src="${user.picture.large}" alt="${user.name.first}" /> |
| 21 | + <div class="user-info"> |
| 22 | + <h4>${user.name.first} ${user.name.last}</h4> |
| 23 | + <p>${user.location.city}, ${user.location.country} </p> |
| 24 | + </div> |
| 25 | + `; |
| 26 | + |
| 27 | + result.appendChild(li); |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +function filterData(searchTerm) { |
| 32 | + listItems.forEach((item) => { |
| 33 | + if (item.innerText.toLowerCase().includes(searchTerm.toLowerCase())) { |
| 34 | + item.classList.remove("hide"); |
| 35 | + } else { |
| 36 | + item.classList.add("hide"); |
| 37 | + } |
| 38 | + }); |
| 39 | +} |
| 40 | + |
| 41 | +// Toggler |
| 42 | +let toggler = document.getElementById("switch"); |
| 43 | + |
| 44 | +toggler.addEventListener("click", () => { |
| 45 | + console.log(toggler.checked); |
| 46 | + if (toggler.checked === true) { |
| 47 | + document.body.style.backgroundColor = "rgb(17, 17, 17)"; |
| 48 | + document.querySelector(".header").style.backgroundColor = "crimson"; |
| 49 | + } else { |
| 50 | + document.body.style.backgroundColor = "white"; |
| 51 | + document.querySelector(".header").style.backgroundColor = "black"; |
| 52 | + } |
| 53 | +}); |
0 commit comments