HTML Question

auto input data depended what i input in html form

Please help me

how to auto input data depended what i input in html form.

example:

in a form have a two field "CITY" AND "ZIPCODE"
i want when anyone type any zip code on zip code field then the city field auto full up

more example:

input zipdcode="1050" auto input in city="dhaka"
input zipcode="3000" auto input in city="barisal"
input zipcode="5000" auto input in city="khulna"


just like this here is a picture
https://drive.google.com/open?id=0BzfHx6Xpsbl4VGFpMGoxSHFPY2s

Answer

Checkout the following

const zipInput = document.querySelector('#zip'),
      cityInput = document.querySelector('#city'),
      zipCityMap = {
          1050: 'dhaka',
          3000: 'barisal',
          5000: 'khulna'
      }

zipInput.addEventListener('keyup', myKeyup);

function myKeyup(e){
   const city = zipCityMap[this.value];
   if(city){
     cityInput.value = city;
   } else {
     cityInput.value = '';
   }
}
<input id="zip" value=""/>
<input id="city" value="" />

Comments