Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I disable selected attribute from select2() dropdown Jquery?

I know how to enable the selected attribute from dropdown; I can use this code :

$('select').select2(); 

but my problem is how to disable it ? thx Click Here

like image 242
Attila Naghi Avatar asked Jan 17 '14 13:01

Attila Naghi


People also ask

How do I make Select2 disabled?

Disabling a Select2 control Select2 will respond to the disabled attribute on <select> elements. You can also initialize Select2 with disabled: true to get the same effect.

How do I hide Select2 search box?

Hiding the search box For single selects, Select2 allows you to hide the search box using the minimumResultsForSearch configuration option. In this example, we use the value Infinity to tell Select2 to never display the search box.

What does Select2 () do?

Select2 gives you a customizable select box with support for searching, tagging, remote data sets, infinite scrolling, and many other highly used options.

How do I create a Select2 dynamically?

HTML. Create a <select class="select2_el" > element to initialize select2 on page load and create <div id="elements" > container to store <select > element on button click using jQuery AJAX.


2 Answers

The right way for Select2 3.x is:

$('select').select2("enable", false) 

This works fine.

like image 169
user4105545 Avatar answered Sep 18 '22 12:09

user4105545


For those using Select2 4.x, you can disable an individual option by doing:

$('select option:selected').prop('disabled', true); 

For those using Select2 4.x, you can disable the entire dropdown with:

$('select').prop('disabled', true); 
like image 43
robert Avatar answered Sep 20 '22 12:09

robert