Filling In Form Values

In this example, we are populating text fields based on information entered in another text field.
Enter a zip code value of 95126, 95011, or 60608 and the city pre-populates for you once you click outside of the form field. Any other value displays a message.

Zip:

City:

Code Example

<script>
 function fillCity() {
 var cityName;
 var zipEntered = document.getElementById("zip").value;

 switch(zipEntered) {
 case "95126" :
    cityName = "San Jose";
    break;
  
 case "95011" :
    cityName = "Campbell";
    break;
  
 case "60608" :
    cityName = "Chicago";
    break;
  
 default :
    cityName = "Sorry, never heard of it."; 	
 }
 document.getElementById("city").value = cityName;
 }
</script>