Skip to content

Instantly share code, notes, and snippets.

@jayde
Created July 4, 2013 16:37
Show Gist options
  • Save jayde/5929003 to your computer and use it in GitHub Desktop.
Save jayde/5929003 to your computer and use it in GitHub Desktop.
Check to see if a <select> has an empty selection/no value.
// Check to see if a <select> has an empty selection/no value
$.fn.checkEmptySelection = function() {
var emptyClass = 'noselection';
$(this).each(function() {
if( $(this).val() == emptySelection ) {
$(this).addClass(emptyClass);
}
});
$(this).on('blur change', function() {
if( $(this).val() == emptySelection ){
$(this).addClass(emptyClass);
}
if( $(this).val() != emptySelection ){
$(this).removeClass(emptyClass);
}
});
$(this).on('focus', function() {
$(this).removeClass('noselection');
});
}
$('select').checkEmptySelection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment