Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
211 views
Welcome To Ask or Share your Answers For Others

1 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="" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...