Skip to content

Instantly share code, notes, and snippets.

@mcavaliere
Last active June 9, 2018 14:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mcavaliere/1c3dfb95ccbe29761543 to your computer and use it in GitHub Desktop.
Save mcavaliere/1c3dfb95ccbe29761543 to your computer and use it in GitHub Desktop.
// Less Coupled
var CountrySelectClass = function() {
var countriesIveBeenTo = {
'BE': 'Belgium',
'CR': 'Costa Rica',
'IT': 'Italy',
'US': 'United States of America',
'UK': 'United Kingdom'
};
this.init = function(selector) {
this.$el = $(selector);
$.each(countriesIveBeenTo, function(val, text) {
this.$el.append(
$('<option value="' + val + '">' + text + '</option>')
);
}.bind(this));
this.$el.change(function() {
console.log('Selected country: ' + this.$el.find(':selected').text());
}.bind(this));
};
};
$(function() {
$(document.body).append( $('<select id="countries"></select>') );
var csInstance = new CountrySelectClass();
csInstance.init("#countries");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment