Skip to content

Instantly share code, notes, and snippets.

@graphoarty
Created November 25, 2023 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graphoarty/55ca889a5ec407eae74466c0ec12d85e to your computer and use it in GitHub Desktop.
Save graphoarty/55ca889a5ec407eae74466c0ec12d85e to your computer and use it in GitHub Desktop.
blur select box after selection (even if value is unchanged)
let selectDropDownActive = false;
let selectDropDownActiveItem = null;
$(window).blur(function() {
BlurActiveSelectBox();
})
$('select').click(function() {
if (selectDropDownActiveItem != this) {
selectDropDownActive = false;
}
if (!selectDropDownActive) {
$(document).off('click', BlurActiveSelectBox);
setTimeout(() => {
$(document).on('click scroll', BlurActiveSelectBox);
selectDropDownActive = true;
selectDropDownActiveItem = this;
}, 100);
}
});
function BlurActiveSelectBox() {
if (selectDropDownActive) {
$('select').blur();
$(document).off('click scroll', BlurActiveSelectBox);
selectDropDownActive = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment