Skip to content

Instantly share code, notes, and snippets.

@lingceng
Last active July 18, 2017 11:32
Show Gist options
  • Save lingceng/d1cd5ca5db9a777b31487769242c422f to your computer and use it in GitHub Desktop.
Save lingceng/d1cd5ca5db9a777b31487769242c422f to your computer and use it in GitHub Desktop.
Make select2 4.0 above tags works with text input field
// https://gist.github.com/lingceng/d1cd5ca5db9a777b31487769242c422f
// See the issue here: https://github.com/select2/select2/issues/3022
function select2InputTags(queryStr) {
var $input = $(queryStr)
var $select = $('<select class="'+ $input.attr('class') + '" multiple="multiple"><select>')
if ($input.val() != "") {
$input.val().split(',').forEach(function(item) {
$select.append('<option value="' + item + '" selected="selected">' + item + '</option>')
});
}
$select.insertAfter($input)
$input.hide()
$select.change(function() {
$input.val(($select.val() || [""]).join(","));
});
return $select;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment