Misc

How to add Custom message into Select2

This tutorial will illustrate to you, how to add a user-defined message into the select2 control. I am using the jQuery Select2 plugin.

The Default message of select2 is ‘No matches found’. Sometimes we need to add a custom message for select2. I will share the script for the 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 the default option.

html += "Add New Number";

The above code will show you the ‘Add New Number’ text when you will render select2.

Related Post

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

matcher: function(term, text) {
   return text === 'Add New Number' || $.fn.select2.defaults.matcher.apply(this, arguments);
}

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

matcher: function(term, text) {
  return text === 'Add New Number' || $.fn.select2.defaults.matcher.apply(this, arguments);
}

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

sortResults: function(results) {
 if (results.length > 1) results.pop();
 return results;
}

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

jQuery("#selectId").select2({
 width: 'element',
 matcher: function(term, text) {
  return text === 'Add New Number' || $.fn.select2.defaults.matcher.apply(this, arguments);
    },
 sortResults: function(results) {
    if (results.length > 1) results.pop();
  return results;
 }
});

Step 5: Result.

custom search message

Demo & Download Source Code

View Comments

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We'll explore laravel table Relationships usage and best practices… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

2 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

3 months ago

Categories