How to add Custom message into Select2

This tutorial will illustrate you, how to add user defined message into select2 control.I am using jQuery Select2 plugin. The Default message of select2 is 'No matches found'. Sometimes we need to add custom message for select2.I will share script for set custom message as well as fire an event on this message.

Also checkout other tutorials of select2,

Step 1: We will add custom option for default option.

Above code will show you ‘Add New Number’ text when you will render select2.

Step 2: Now we will add condition to show above option into select2 dropdown list, when the search string does not found into select2 then we will show ‘Add New Number’ instead of default message.

}
Step 3: Remove this option at the time of select2 binding.

So ‘Add New Number’ will not show when the first time select2 render, it will show when the matching character does not find.

Step 4: The Full code of select2 with custom option.

Step 5: Result.

custom search message

Demo & Download Source Code