Skip to content

Commit 878bb3a

Browse files
pquentinkevin-brown
authored andcommitted
Fix select2('') calls on multiple elements
This fixes an issue when any of Select2's special options are called on multiple elements, it would only affect the first option in the group. This was because Select2 was only applying any changes to the first element in the group (as chosen by jQuery) instead of applying changes on each and every element within the list. This has the new side effect of special options like `select2('data')` returning the results for the last element in the list instead of the first element. Because the previous functionality was considered unspecified behaviour, this is not being treated as a breaking change. This closes select2#3413 This closes select2#3495
1 parent 7c47912 commit 878bb3a

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

src/js/jquery.select2.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,22 @@ define([
2424

2525
return this;
2626
} else if (typeof options === 'string') {
27-
var instance = this.data('select2');
27+
var ret;
2828

29-
if (instance == null && window.console && console.error) {
30-
console.error(
31-
'The select2(\'' + options + '\') method was called on an ' +
32-
'element that is not using Select2.'
33-
);
34-
}
29+
this.each(function () {
30+
var instance = $(this).data('select2');
3531

36-
var args = Array.prototype.slice.call(arguments, 1);
32+
if (instance == null && window.console && console.error) {
33+
console.error(
34+
'The select2(\'' + options + '\') method was called on an ' +
35+
'element that is not using Select2.'
36+
);
37+
}
3738

38-
var ret = instance[options].apply(instance, args);
39+
var args = Array.prototype.slice.call(arguments, 1);
40+
41+
ret = instance[options].apply(instance, args);
42+
});
3943

4044
// Check if we should be returning `this`
4145
if ($.inArray(options, thisMethods) > -1) {

0 commit comments

Comments
 (0)