Skip to content

Commit 4dce487

Browse files
committed
Prevent IE from generating unwanted clicks on the body. When mousedown fires on the selection element and then mouseup fires on the mask (because it is moved under the mouse during mousedown) then IE fires a click event on the body because it is the closest common ancestor. To prevent this, we detach and reatach the elements on mousedown which cancels the click event.
1 parent 3e5ad13 commit 4dce487

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

select2.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ the specific language governing permissions and limitations under the Apache Lic
105105
nextUid=(function() { var counter=1; return function() { return counter++; }; }());
106106

107107

108+
function reinsertElement(element) {
109+
var placeholder = $(document.createTextNode(''));
110+
111+
element.before(placeholder);
112+
placeholder.before(element);
113+
placeholder.remove();
114+
}
115+
108116
function stripDiacritics(str) {
109117
var ret, i, l, c;
110118

@@ -1296,6 +1304,9 @@ the specific language governing permissions and limitations under the Apache Lic
12961304
mask.hide();
12971305
mask.appendTo(this.body());
12981306
mask.on("mousedown touchstart click", function (e) {
1307+
// Prevent IE from generating a click event on the body
1308+
reinsertElement(mask);
1309+
12991310
var dropdown = $("#select2-drop"), self;
13001311
if (dropdown.length > 0) {
13011312
self=dropdown.data("select2");
@@ -2048,6 +2059,8 @@ the specific language governing permissions and limitations under the Apache Lic
20482059
}));
20492060

20502061
selection.on("mousedown touchstart", this.bind(function (e) {
2062+
// Prevent IE from generating a click event on the body
2063+
reinsertElement(selection);
20512064

20522065
if (!this.container.hasClass("select2-container-active")) {
20532066
this.opts.element.trigger($.Event("select2-focus"));
@@ -2730,7 +2743,7 @@ the specific language governing permissions and limitations under the Apache Lic
27302743
this.search.select();
27312744
}
27322745
}
2733-
2746+
27342747
this.updateResults(true);
27352748
this.search.focus();
27362749
this.opts.element.trigger($.Event("select2-open"));
@@ -2797,7 +2810,7 @@ the specific language governing permissions and limitations under the Apache Lic
27972810

27982811
// keep track of the search's value before it gets cleared
27992812
this.nextSearchTerm = this.opts.nextSearchTerm(data, this.search.val());
2800-
2813+
28012814
this.clearSearch();
28022815
this.updateResults();
28032816

0 commit comments

Comments
 (0)