Created
July 27, 2019 10:03
-
-
Save sambenne/c5e4173114299b693e3dc97ffe14d962 to your computer and use it in GitHub Desktop.
Show the OPTGROUP value with the selected value
This file contains 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
$('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