Activity 2

 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 State Province">
    <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="Activity2.js"></script>
</body>
</html>

CSS

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

let btn = document.querySelector("button");
btn.addEventListener("click", async () => {
  let state = document.querySelector("input").value;
  let colleges = await getCollege(state);
  show(colleges);
});

async function getCollege(state) {
  try {
    let res = await axios.get(url + state);
    console.log(res.data);
    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