Created
August 14, 2012 08:24
-
-
Save netsi1964/3347519 to your computer and use it in GitHub Desktop.
jQuery get danish cityname from zipcode
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
// http://gist.github.com/3347519 | |
function getCityFromZipCode(sZipcodeSelector, sTargetSelector, sLoadingClass) { | |
var sLoadingClass = (sLoadingClass||'loading'); | |
var $sTargetSelector = jQuery(sTargetSelector); | |
var $sZipcodeSelector = jQuery(sZipcodeSelector); | |
if ($sTargetSelector.length>0 && $sZipcodeSelector.length>0) { | |
var sZipCode = $sZipcodeSelector.val(); | |
$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.val(data.navn); | |
$sTargetSelector.removeClass(sLoadingClass); | |
} | |
}); | |
return true; | |
} else { | |
return false; | |
} | |
} |
Author
netsi1964
commented
Sep 16, 2012
<iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/netsi1964/kMSFr/embedded/" allowfullscreen="allowfullscreen" frameborder="0"></iframe>
You may find a test implementation on jsFiddle.net here: http://jsfiddle.net/netsi1964/kMSFr/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment