jqueryjquery-select2jquery-select2-4

How do I allow spaces when using Select2 tags


I'm using JQuery 1.10.2 and Select2 4.0.4 with the 'tags' option turned on.

$(this).select2({ 
   placeHolder: "Select One",
   allowClear: true, 
   tags: true, 
   tokenSeparators: [',']
});

What isn't working is if I try to type "New York" the space is ignored and a new tag is created as "NewYork". It's like I didn't type it. Is there a way to allow spaces in new 'tags' in Select2 options?


Solution

  • Don't propagate change event from the search field out of the component, I went throw the select2.min.js file and I have commented this validation:

    n.trigger("keypress",e),e.which===i.SPACE&&e.stopPropagation()
    

    or you can replace it with:

    n.trigger("keypress",e),e.which===i.SPACE&&e.stopPropagation()