Skip to content

Instantly share code, notes, and snippets.

@sergiors
Last active August 29, 2015 14:24
Show Gist options
  • Save sergiors/72c734eb821f0e5eeebe to your computer and use it in GitHub Desktop.
Save sergiors/72c734eb821f0e5eeebe to your computer and use it in GitHub Desktop.
// see before https://github.com/sergiors/vanilla
(function() {
'use strict';
var state = document.getElementById('address_state');
state.on('change', function() {
var city = document.getElementById('address_city');
city.setAttribute('disabled', true);
if (!state.value) {
return;
}
$.getJSON('http://example.com/cities', {
state: state.value
}, function(data) {
var select = document.createElement('select');
select.setAttributes(city.getAttributes());
for (var key in data) {
var option = document.createElement('option');
option.value = data[key].id;
option.text = data[key].name;
select.options.add(option);
}
select.removeAttribute('disabled');
city.outerHTML = select.outerHTML;
});
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment