flurin-d
1/26/2016 - 7:29 AM

autofill place by plz

Shows the place matching the entered postcode using the value in #postcode using the postal Code Lookup Service from geonames.org.

/* AUTOFILL PLZ/PLACE
/----------------------------*/
$('#postcode').keyup(function() {
  if ($(this).val().length > 3) {
    var $location = $('#city');
    $.getJSON('http://www.geonames.org/postalCodeLookupJSON?&country=CH&callback=?', {
      postalcode: this.value
    }, function(response) {
      if (response && response.postalcodes.length && response.postalcodes[0].placeName) {
        $location.val(response.postalcodes[0].placeName);
      }
    });
  } 
  else {
    $('#city').val('');
  }
});