Last active
October 24, 2023 18:46
-
-
Save ben-caplan/9260363 to your computer and use it in GitHub Desktop.
Styled Select JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//CUSTOM SELECT - prevent double wrap incase this script is include in multiple locations... | |
$('select').each(function(){ | |
var t = $(this); | |
if( !t.parent().hasClass('customSelect') ){ | |
t.wrap('<div class="customSelect" />').before('<div class="displayCopy empty"></div>'); | |
} | |
}).on('change fauxChange', function(){ | |
var t = $(this); | |
t.siblings('.displayCopy').text( t.find('option:selected').text() ); | |
//toggle "empty" class | |
if( t.prop('selectedIndex') > 0 ) t.blur().siblings('.displayCopy').removeClass('empty').parent().removeClass('nothing_selected'); | |
else t.siblings('.displayCopy').addClass('empty').parent().addClass('nothing_selected'); | |
}).trigger('fauxChange');//custom select |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment