Skip to content

Instantly share code, notes, and snippets.

@sambenne
Created July 27, 2019 10:03
Show Gist options
  • Save sambenne/c5e4173114299b693e3dc97ffe14d962 to your computer and use it in GitHub Desktop.
Save sambenne/c5e4173114299b693e3dc97ffe14d962 to your computer and use it in GitHub Desktop.
Show the OPTGROUP value with the selected value
$('select').each(function () {
if($(this).find('optgroup').length === 0) {
return;
}
$(this).bind("change blur", function () {
let opt = $(this).find(':selected');
let sel = (opt.text().split(' - '))[0];
let og = opt.closest('optgroup').attr('label');
$(this).find(':selected').text(sel + (og === undefined ? '' : ' - ' + og));
});
$(this).focus(function () {
$(this).find('option').each(function(){
$(this).text(($(this).text().split(' - '))[0]);
});
});
let opt = $(this).find(':selected');
let sel = opt.text();
let og = opt.closest('optgroup').attr('label');
$(this).find(':selected').text(sel + (og === undefined ? '' : ' - ' + og));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment