Created
April 10, 2013 16:39
-
-
Save jalalhejazi/5356289 to your computer and use it in GitHub Desktop.
JavaScript: getCityFromZipCode oiorest/postnummer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCityFromZipCode(sZipcodeSelector, sTargetSelector) { | |
var sLoadingClass = 'loading'; | |
var $sTargetSelector = jQuery(sTargetSelector); | |
var $sZipcodeSelector = jQuery(sZipcodeSelector); | |
$sTargetSelector.html(""); | |
if ($sTargetSelector.length > 0 && $sZipcodeSelector.length > 0 ) { | |
var sZipCode = $sZipcodeSelector.val(); | |
//DK post-nummer er en string med længden på 4 | |
//For at undgå unødvendig $.ajax call til serveren. | |
if (sZipCode.length == 4) { | |
$sTargetSelector.removeClass(sLoadingClass).addClass(sLoadingClass); | |
$.ajax({ | |
url: 'http://geo.oiorest.dk/postnumre/' + sZipCode + '.json?callback=?', | |
method: 'get', | |
dataType: 'json', | |
crossDomain: true, | |
success: function(data) { | |
$sTargetSelector.html(data.navn); | |
$sTargetSelector.removeClass(sLoadingClass); | |
} | |
}); | |
return true; | |
} | |
} else { | |
return false; | |
} | |
} | |
$(function() { | |
$('#postNummer').focus(); | |
$('#postNummer').on('keyup', function() { | |
getCityFromZipCode('#postNummer', '#byNavn'); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Virker ikke mere...