Created
March 3, 2016 16:27
-
-
Save irisfofs/30f0251b9a360601c1d4 to your computer and use it in GitHub Desktop.
Terrible workaround for weird select2 bug
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
// For some reason select2 interferes with the space key for multiple | |
// selects. It doesn't in its own examples, but it does here. This is a | |
// workaround to manually add a space to the input. It uses JQuery event | |
// delegation because the actual element that the event is triggered on | |
// doesn't exist at the time this gets set up. | |
element.parent().on('keypress', '.select2-selection', awfulSpaceHack); | |
element.on('$destroy', function () { | |
element.parent().off('keypress', '.select2-selection', awfulSpaceHack); | |
}); | |
function awfulSpaceHack(evt) { | |
if (evt.which === 32) { // 32 is the space character | |
var val = $(evt.target).val(); | |
$(evt.target).val(val + ' '); // gr8 style | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment