index.html 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. </head>
  9. <body>
  10. <form action="">
  11. <select id="countrySelect" name=""></select>
  12. <select id="citySelect" name=""></select>
  13. </form>
  14. <script>
  15. //1
  16. fetch('https://raw.githubusercontent.com/russ666/all-countries-and-cities-json/master/countries.json')
  17. .then(res => res.json())
  18. .then(json => {
  19. let jsObj = json
  20. for (let key in jsObj) {
  21. let countryOption = document.createElement('option');
  22. let countrySelect = document.getElementById('countrySelect');
  23. countryOption.innerHTML = key;
  24. countrySelect.appendChild(countryOption);
  25. }
  26. document.querySelector('#countrySelect').addEventListener('change', function () { // Замыкание
  27. let cities = jsObj[this.value]
  28. citySelect.length = 0;
  29. for (const iterator of cities) {
  30. let cityOption = document.createElement('option');
  31. let citySelect = document.getElementById('citySelect');
  32. cityOption.innerHTML = iterator;
  33. citySelect.appendChild(cityOption);
  34. }
  35. })
  36. })
  37. </script>
  38. </body>
  39. </html>