246

For my more significant selects, the search box in Select2 is wonderful. However, in one instance, I have a simple selection of 4 hard-coded choices. In this case, the search box is superfluous and looks a little silly being present. Is it possible to hide it somehow? I took a look through the documentation online and couldn't find any options for this in the constructor.

I could, of course, just use a regular HTML select, but for consistency, I'd like to use Select2 if possible.

EleventyOne
  • 7,300
  • 10
  • 35
  • 40
  • Thank you for the idea. Although I knew about .show() and .hide() in jQuery, it didn't occur to me that the plug-in would continue to work if I did something like that outside of its own options. Nevertheless, at first glance, it seems to work :) – EleventyOne Jul 05 '13 at 02:03
  • Can you explain what the hide method does and how it works? Are you telling me it only hides the search box because that does not seem to be the case at all – IcedDante Nov 13 '14 at 16:46
  • @IcedDante The `hide` method is described here: http://api.jquery.com/hide/ As long as you only apply it to the appropriate selector then, yes, it should hide the search box alone. Nevertheless, there are plugin-specific methods that can be employed to hide it, which I would recommend you use instead (see below). – EleventyOne Mar 22 '15 at 20:34

21 Answers21

554

See this thread https://github.com/ivaynberg/select2/issues/489, you can hide the search box by setting minimumResultsForSearch to a negative value.

$('select').select2({
    minimumResultsForSearch: -1
});
Blue Smith
  • 8,580
  • 2
  • 28
  • 33
  • 17
    The main issue here as mentionned in the ticket on github is on mobile it still display the keyboard. We have replaced select2 with chosen. – toutpt Oct 24 '13 at 08:43
  • 2
    and it translates perfectly to angular-ui ui-select2 ! – rhigdon Oct 24 '13 at 22:53
  • Very handy option! I used it to show search for 10 and more options. No need for manual removing search input. – blazkovicz Aug 12 '14 at 11:03
  • @KevinBrown `Infinity` is not a negative value. Your edit makes the text and the code not match up. The issue linked to specifically points out that a negative value is the correct supported approach. The issue linked to also claims "High value of minimumResultsForSearch only hides searchbox in opened select. But if we type some letter while select is closed and focused - search will pop up, no matter how high is" and although I cannot reproduce that particular problem in the current version, it is likely to be a real problem in older versions. For those reasons, I rolled back your edit. –  Oct 05 '15 at 08:29
  • When using an ajax call to initially populate the dropdown, this hides the search box after the results are rendered. Up until then it shows the box while 'searching' is displayed and data is loaded. – Andrew Aug 08 '17 at 16:30
  • 1
    But, it completely disables the search functionality. – sudip Feb 14 '19 at 20:44
  • This does not work on select2 V4. For V4 use minimumResultsForSearch: "Infinity". – ilibilibom Feb 19 '19 at 12:21
  • This is very helpful when you're encountering search bar showing up on page load. – Mark Salvania Jun 04 '19 at 12:52
  • Works with ng-select2 as well. – Brandon Nov 30 '21 at 13:42
56

It's on their Examples page: https://select2.org/searching#hiding-the-search-box

$(".js-example-basic-hide-search").select2({
  minimumResultsForSearch: Infinity
});
Scoots
  • 3,048
  • 2
  • 21
  • 33
Sabin
  • 747
  • 5
  • 10
42

Version 4.0.3

Try not to mix user interface requirements with your JavaScript code.

You can hide the search box in the markup with the attribute:

data-minimum-results-for-search="Infinity"

Markup

<select class="select2" data-minimum-results-for-search="Infinity"></select>

Example

$(document).ready(function() {
  $(".select2").select2();
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

<label>without search box</label>
<select class="select2" data-width="100%" data-minimum-results-for-search="Infinity">
  <option>one</option>
  <option>two</option>
</select>

<label>with search box</label>
<select class="select2" data-width="100%">
  <option>one</option>
  <option>two</option>
</select>
Aaron Hudon
  • 5,280
  • 4
  • 53
  • 60
40
.no-search .select2-search {
    display:none
}

$("#test").select2({
    dropdownCssClass : 'no-search'
}); 
xicooc
  • 855
  • 7
  • 8
  • 1
    +1 I like this as it prevents the search box from briefly appearing in the UI while a AJAX request is made. this is also true for the -1 hack. – SimonGates Oct 06 '14 at 14:44
  • This is definitly best answer for the question because it really prevent event ui such as "searching....". – Sarin Suriyakoon Jun 30 '15 at 07:50
  • 6
    Worked perfectly, thank you! Just a note to any future Googler's, this requires the select2 full version (select2.full.*), as stated here: https://github.com/select2/select2/issues/2879#issuecomment-149940634 – Othyn Feb 12 '16 at 10:56
  • 1
    Thanks, it's what I was looking for since even with search box hidden it allows to keep search functionnality available when you wish to call it programmatically : $("#myselect").select2("search","search_value") – nicolas Oct 06 '16 at 15:04
  • Indeed this is the best option. As @Othyn says the full version is needed select2.full.min.js as documented on the website: https://select2.github.io/ – robbpriestley May 10 '17 at 17:06
21

Removing the inputs with jQuery works for me:

$(".select2-search, .select2-focusser").remove();
Arkaitz Garro
  • 529
  • 4
  • 5
6

You can set either infinity or -1 for minimumResultsForSearch option

Solution 1

Ref: https://select2.org/searching#limiting-display-of-the-search-box-to-large-result-sets

$('select').select2({
    minimumResultsForSearch: -1
});

Solution 2

Ref: https://select2.org/searching#hiding-the-search-box

$('select').select2({
   minimumResultsForSearch: Infinity
});
Soubhagya Kumar Barik
  • 1,979
  • 20
  • 26
5

If you want to hide search for a specific drop down use the id attribute for that.

$('#select_id').select2({ minimumResultsForSearch: -1 });
Pradeep
  • 9,667
  • 13
  • 27
  • 34
Saifur Rahman
  • 144
  • 2
  • 14
4

This is the best solution, clean and work good :

$("#select2Id").select2 () ;
$("#select2Id").select2 ('container').find ('.select2-search').addClass ('hidden') ;

Then, create a class .hidden { display;none; }

rene
  • 41,474
  • 78
  • 114
  • 152
YWA
  • 41
  • 1
3

You can try this

$('#select_id').select2({ minimumResultsForSearch: -1 });

it closes the search results box and then set control unvisible

You can check here in select2 document select2 documents

Foram
  • 483
  • 5
  • 12
1

I like to do this dynamically depending on the number of options in the select; to hide the search for selects with 10 or fewer results, I do:

$fewResults = $("select>option:nth-child(11)").closest("select");
$fewResults.select2();
$('select').not($fewResults).select2({ minimumResultsForSearch : -1 });
Mike Campbell
  • 7,921
  • 2
  • 38
  • 51
1
//readonly on all select2 input
$(".select2-search input").prop("readonly", true);
1
//Disable a search on particular selector
$(".my_selector").select2({
    placeholder: "ÁREAS(todas)",
    tags: true,
    width:'100%',
    containerCssClass: "area_disable_search_input" // I add new class 
});

//readonly prop to selector class
$(".area_disable_search_input input").prop("readonly", true);
1

If you want to hide on initial opening and you are populating the dropdown via ajax call, add the following to the ajax block in your select2 declaration:

beforeSend: function () 
  {
    $('.select2-search--dropdown').addClass('hidden');
  }

To then show it again (and focus) after your ajax request is successful:

  success: function() {
      $('.select2-search--dropdown').removeClass('select2-search--hide'); // show search bar then focus
      $('.select2-search__field')[0].focus();
  }
Tom_B
  • 307
  • 2
  • 8
Andrew
  • 253
  • 2
  • 14
1

If you have multi attribute in your select, this dirty hack works:

var multipleSelect = $('select[name="list_type"]');
multipleSelect.select2();
multipleSelect.parent().find('.select2-search--inline').remove();
multipleSelect.on('change', function(){
    multipleSelect.parent().find('.select2-search--inline').remove();
});

see docs here https://select2.org/searching#limiting-display-of-the-search-box-to-large-result-sets

1

try this CSS

input[aria-controls=select2-product-type-results]{
  display: none;
}

this input is search field

Vajiheh Habibi
  • 379
  • 1
  • 4
  • 16
1

Just in case someone stumbles on this, there is now a hideSearch option in the constructor which seems closer to what the question was looking for.

echo Select2::widget([ 'name' => 'status','hideSearch' => true, 'data' => [1 => 'Active', 2 => 'Inactive']]);

1

$(document).ready(function() {
  $(".select2").select2();
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>

<label>without search box</label>
<select class="select2" data-width="100%" data-minimum-results-for-search="Infinity">
  <option>one</option>
  <option>two</option>
</select>

<label>with search box</label>
<select class="select2" data-width="100%">
  <option>one</option>
  <option>two</option>
</select>
0

If the select is show results one have to use this:

$('#yourSelect2ControlId').select2("close").parent().hide();

it closes the search results box and then set control unvisible

MKK
  • 2,431
  • 20
  • 16
  • This will hide entire select field. As a note, the select2 container is inserted into the DOM after click on select. So we cannot bind an event to it. The working solution is the accepted one: `minimumResultsForSearch: -1`. – LucianDex Jun 29 '22 at 05:36
0

@Misha Kobrin's answer work well for me So I have decided to explain it more

You want to hide the search box you can do it by jQuery.

for example you have initialized select2 plugin on a drop down having id audience

element_select = '#audience';// id or class
$(element_select).select2("close").parent().hide();

The example works on all devices on which select2 works.

Shahbaz
  • 3,433
  • 1
  • 26
  • 43
0

I edited the select2.min.js file to set the select-2__search input field that's generated to readonly="true".

capcom
  • 525
  • 7
  • 16
0

For multiselect you have to write js code, there is no settings property.

$('#js-example-basic-hide-search-multi').select2();

$('#js-example-basic-hide-search-multi').on('select2:opening select2:closing', function( event ) {
    var $searchfield = $(this).parent().find('.select2-search__field');
    $searchfield.prop('disabled', true);
});

This mentioned on their page: https://select2.org/searching#multi-select

Faisal Ghaffar
  • 181
  • 2
  • 12