11_practic.html 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>Document</title>
  7. </head>
  8. <body>
  9. <select name="country" id="countrySelect">
  10. Country
  11. </select>
  12. <select name="city" id="citySelect">
  13. City
  14. </select>
  15. <script>
  16. let newOption = function (someValue) {
  17. let op = document.createElement("option");
  18. op.setAttribute("value", someValue);
  19. op.append(someValue);
  20. return op;
  21. };
  22. fetch("https://raw.githubusercontent.com/russ666/all-countries-and-cities-json/master/countries.json")
  23. .then((res) => res.json())
  24. .then((data) => {
  25. console.log(data);
  26. for (let country in data) countrySelect.append(newOption(country));
  27. countrySelect.onchange = () => {
  28. citySelect.innerHTML = "";
  29. for (let city of data[countrySelect.value]) citySelect.append(newOption(city));
  30. };
  31. countrySelect.onchange();
  32. });
  33. console.log(1);
  34. </script>
  35. </body>
  36. </html>