forked from Ziptastic/ziptastic-jquery-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzippidydoda.js
61 lines (42 loc) · 1.37 KB
/
zippidydoda.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
(function( $ ) {
$.fn.ziptastic = function( options ) {
// Create some defaults, extending them with any options that were provided
var settings = $.extend( {
'zip' : 'zip',
'cityid' : 'city',
'stateid' : 'state',
'countryid' : 'country'
}, options);
var zip_element = $("#" + settings.zipid);
var city_element = $("#" + settings.cityid);
var state_element = $("#" + settings.stateid);
var country_element = $("#" + settings.countryid);
// Hide the elements that we're going to retreive data for.
city_element.parent().hide();
state_element.parent().hide();
country_element.parent().hide();
$('#zip').keyup(function() {
if ($(this).val().match(/^\d{5}$/) == null) {
// This isn't a real zip, so do nothing
return;
}
var client = new XMLHttpRequest();
client.open("GET", "http://localhost?zip=" + this.value, true);
client.onreadystatechange = function() {
if(client.readyState == 4) {
//alert(client.responseText);
var location_data = JSON.parse(client.responseText);
city_element.val(location_data.city);
city_element.parent().show(500);
state_element.val(location_data.state);
state_element.parent().show(500);
country_element.val(location_data.country);
country_element.parent().show(500);
};
};
client.send();
});
/*
*/
};
})( jQuery );