Skip to content

Instantly share code, notes, and snippets.

@xiagu
Created March 3, 2016 16:27
Show Gist options
  • Save xiagu/30f0251b9a360601c1d4 to your computer and use it in GitHub Desktop.
Save xiagu/30f0251b9a360601c1d4 to your computer and use it in GitHub Desktop.
Terrible workaround for weird select2 bug
// 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