Next Search Term

Select2 is a great alternative for the standard select box (check it out: Select2).

I worked on a project that required a specific behavior to be added to Select2.
I needed a way to display the currently selected value inside the search box in order to avoid having the user type it in again over and over again.
My plan was to add several Select2 on the same web page containing up to 10.000 options each and I wanted a way to have the current selected value available for edit and easy copy/pasting to the user.

So I added a helper method to support this functionality which was pulled into the Select2's 3.5.2 stable build (search for nextSearchTerm on the documentation page.

Following examples show you how to display the currently selected value and the current search term. View the page source to review the JavaScript code.

Display selected value


Display current search term


PS: If you are using a more recent version of Select2 (and you should), you probably don't need this helper method anymore, you can accomplish the same behavior using its built-in features.