Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select2 will not disable or clear

I have a secondary select slaved to a primary select (pick a store, then pick a department - identical to pick a country, then pick a state).

I absolutely cannot get select2's ('enable',false) and ('data', null) methods to work, no matter how much other code I tear out.

<select id="stores">
  <option value="foo">foo</option>
</select>
<select id="depts">
  <option value="bar">bar</option>
</select>

// ...some logic that selects a store, then fetches that store's depts ...

$('#depts').select2('enable',false);// does not disable control
$('#depts').select2('data',null); // does not clear control

So I'm forced to do this:

$('select#depts').empty(); // clears HTML element
$('#depts').select2(); // re-enhances select w/ select2
$('#depts').select2('disable',true); // disables 

It behaves itself in jsfiddle, so I can't even post an example and request help. I'm just ... stumped.

like image 419
DaveC426913 Avatar asked Jul 16 '13 15:07

DaveC426913


People also ask

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 know if Select2 is open?

select2:open is fired whenever the dropdown is opened. select2:opening is fired before this and can be prevented. select2:close is fired whenever the dropdown is closed. select2:closing is fired before this and can be prevented.


1 Answers

This code should work in all browsers (select2 v4.0.4):

To disable:

$('select').prop('disabled', true).trigger('change');

To enable:

$('select').prop('disabled', false).trigger('change');
like image 109
wp-mario.ru Avatar answered Oct 21 '22 17:10

wp-mario.ru