How do i search options via javascript


#1

Is there a way to search select2 options by label via javascript.
Like:
$(’#id’).select2(‘search’,‘text’);


#2

Select2 doesn’t provide a programmatic search capability. You can do it manually, though, by setting the value of the Select2’s input field to the search term you want. Something like this will work:

$("#mySelect").select2("open");
$("input.select2-search__field").eq(0).val(searchTerm).trigger("input");

The first line programmatically opens the the Select2, exposing the search field. The second line sets the value of the search field to the desired search term and triggers the input event, which causes the Select2 to display the items that match the value of searchTerm.