Feature: Locked Items #503

merged 3 commits into from Oct 24, 2012

8 participants


Per our discussion previously, I've renamed disabled to locked instead.

An example of the code/feature can be found here:


will merge this when i have time to also update the docs with an example and the description. if you have time to do that in gh-pages branch would be great....

@ivaynberg ivaynberg merged commit 4a363c4 into select2:master Oct 24, 2012

how would the lock work if i am loading my items dynamically, and not in javascript?

Select2 member

For Ajax, in the results you would include a locked property for each value which would be either true or false.


Kevin is correct. The ajax would return { id: 1, text: 'blah', locked: true } and IF the user selected it, they won't be able to remove it ( fair warning! :D )


Not sure, I believe this feature is only for multi: yes - I believe you can pass the disabled="disabled" on a regular single select. I believe select2 honors the disabled attribute of a single select drop down.

Select2 member

Locked items don't work with <select multiple> at the moment, it only works with <input type="hidden">.

One option would be to turn this into a function, with parameters similar to formatResult or formatSelection, that allows the user to create their own function to determine what is locked. The current implementation would be kept as the default function in that case.


After re-reading documentation, select2 does support <select multiple> and Kevin is correct. I'll take a poke at my feature sometime and see if I can get it working with <select muliptle> with an attribute or something (example: <option value="whatever" locked>whatever</option> Not sure if that would be the best way to go about it though and Kevin's suggestion might be faster.


Thanks to both - just to add something here - I would think that the lock feature should apply mainly to a multiple select, as one would want to lock an item in a list (multiple select, and not a single select)....
my scenario is such that I want some items to appear by default at initialisation time/start time, and these items be locked, so they cannot be removed by the user i.e. there is always a default selection, and then if someone selects from the list, then the locked item gets deselected programmatically...



This was the was the reason the locked property was added--so that some defaults could be put in that can not be removed. I don't think it's particular useful for normal selections, just for selections you want to load as defaults.


I'd recommend using the attribute "data-locked" as the attribute to pass in the locked status via the element.


Hi, guys. Is there any chance that "data-locked" attribute will be implemented? I need functionality like "locked: true", but statically, without scripting. My situation: user only can add new items to multiselect, but never can remove already added. So I must show which items are already added, but must not allow to remove it. All HTML with and is generated at server side.


As far as I can tell from looking at the 3.4.0 source code, you can specify locked on an HTML option element as follows:

<option value="1" selected="selected" locked="locked">United States</option>

This works. However, it leads to invalid HTML5.

As suggested by @dswitzer and @WindBridges, using a data-locked attribute would be more appropriate.

Line 776 of select2.js could be changed as follows:

--- docroot/vendor/select2/3.4.0/select2.js 2013-05-14 08:27:46.000000000 -0700
+++ docroot/vendor/select2/3.4.0.HACKED/select2.js  2013-06-23 16:52:28.636936609 -0700
@@ -773,7 +773,7 @@
                     element: element.get(),
                     css: element.attr("class"),
                     disabled: element.prop("disabled"),
-                    locked: equal(element.attr("locked"), "locked")
+                    locked: (equal(element.attr("locked"), "locked") || equal(element.attr("data-locked"), "true") || equal(element.data("locked"), true))
             } else if (element.is("optgroup")) {
                 return {

Then you could change the HTML option as follows:

<option value="1" selected="selected" data-locked="true">TOTAL US - XAOC</option>

I only performed limited testing, but it seemed to work nicely in Chrome and Firefox.

Could one of the devs on the project verify and merge?


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment