This feature is not built in to Select2 (for good reason, IMHO; I think it would be confusing, especially if you don’t specify the “allowClear” configuration option). However, if you want to implement this feature yourself, I think there are a few ways you could approach it.
Probably the “most correct” way would be to supply your own SelectAdapter that overrides the DropdownSearch Decorator. Unfortunately there’s not much “official” documentation about creating your own Adapters and Decorators, but I did come across a decent tutorial on it. And you might find other examples via a Google search for “select2 adapter example”.
If you don’t want to tackle custom adapters and decorators, you can “hack” a reasonable solution using Select2 events, specifically
select2:closing (wherein you could capture the current search text) and
select2:opening* (wherein you could “stuff” the captured text back into the search box). The search box is a regular HTML
<input> element with classname “select2-search__field”. It should be straightforward to find this element in the DOM to capture and set its value. (*Note: having never tried this, I’m not sure “when” the Select2 widget clears the search field—after it’s closed or before it opens. You might need to use the
select2:open event handler instead.)
Although I don’t know your specific use case, I strongly advise you to pre-select the “stuffed” value so the user can easily replace it by simply starting to type, rather than having to delete the “stuffed” text first.