Skip to content

Instantly share code, notes, and snippets.

@DougSisk
Created January 8, 2015 15:28
Show Gist options
  • Save DougSisk/f69a063e9ae960889c28 to your computer and use it in GitHub Desktop.
Save DougSisk/f69a063e9ae960889c28 to your computer and use it in GitHub Desktop.
jQuery Dropdown Plugin
/*
<div class="select">
<div class="selected"></div>
<select>
<option>Option</option>
</select>
</div>
*/
(function($) {
$.fn.dropdown = function() {
return this.each(function() {
var top = $(this);
var select = $(this).find('select');
var selected = $(this).find('.selected');
selected.text(select.find('option:selected').text());
if(select.val())
$(this).addClass('has-value');
select.on('change', function(){
var currentVal = $(this).find('option:selected').text();
selected.text(currentVal);
top.addClass('has-value');
top.removeClass('has-error');
});
});
};
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment