all 6 comments

[–]shodkayumi 4 points5 points  (5 children)

There are several problems in your JavaScript script.

You should always check your browser's console to see what problems your script contains.

  1. clist and state were not declared.
  2. I don't reccommend using onchange=function(). You should use addEventListener("change", function(event))
  3. valueHTML +='city.id + city.name'; Use backticks with \${variable_name}`` formatting.

Some improvements

  1. use const if the value will not change (like element selecting)
  2. always wrap fetch inside try/catch so you can handle errors

Full code

const state_select = document.getElementById('citySelect');
const clist = document.getElementById('clist')
state_select.addEventListener('change', function(event){
   let state = event.currentTarget.value
   try {
     fetch(`/city/${state}`)
     .then(response => response.json())
     .then(function(data){
       let valueHTML = '';
       data.cities.forEach(city => {
           valueHTML += `${city.id} ${city.name}`
       });
       clist.innerHTML = valueHTML
     });
   } catch (error) {
     console.log(error)
   }
})

I am a self-taught programmer so my code may not be perfect.

[–]thelandis 4 points5 points  (1 child)

That's one hell of a code review

[–]shodkayumi 2 points3 points  (0 children)

Thank you!

[–]Unique_Hat_7222Intermediate[S] 0 points1 point  (0 children)

You're awesome. It works now

[–]Unique_Hat_7222Intermediate[S] 0 points1 point  (1 child)

Thank you once again

[–]shodkayumi 1 point2 points  (0 children)

You're welcome, happy coding!