Activity using query Strings

 HTML

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Activity</title>
</head>
<body>
    <input placeholder="Enter Country">
    <button>Search</button>
    <ul id="list"></ul>
    <script src="https://cdn.jsdelivr.net/npm/axios@1.6.7/dist/axios.min.js"></script>
    <script src="Activity.js"></script>
</body>
</html>

CSS

let url = "http://universities.hipolabs.com/search?name=";

let btn = document.querySelector("button");
btn.addEventListener("click", async () => {
  let country = document.querySelector("input").value;
  let colleges = await getCollege(country);
  show(colleges);
});
async function getCollege(country) {
  try {
    let res = await axios.get(url + country);
    console.log(res);
    return res.data;
  } catch (e) {
    console.log(e);
    return "error";
  }
}

function show(colleges) {
  let list = document.querySelector("#list");
  list.innerText = "";
  for (col of colleges) {
    console.log(col.name);

    let li = document.createElement("li");
    li.innerText = col.name;
    list.appendChild(li);
  }

}


Comments

Popular posts from this blog

Callback Hell

TODO list with DOM

Refactoring callback hell code