select2 weird behavior upon clicking
Hi, i used select2 a lot but now, i'm having a weird behavior using
select2 single.
here are the scenario,
i noticed that when i click or open the select using a mouse, it opens the options but immediately closes the options when mouseup event happen. seems like my mouseup event is triggering the selection automatically.
but when i open the select2 by focusing it then opening using enter key it behaves normal like it used to.
i tried to attach mouseup function in the select2 element like this
$('#select2Example').select2({
placeholder: 'Select One',
}).on('mouseup', function(e) {
e.preventDefault();
// i also tried return false but nothing happen.
});
btw, i used bootstrap-flat of designmodo and since bootstrap-flat is using the old version of select2,
i deleted the $.fn.select2 object and then import the latest version of select2
like this.
<script src='path/to/bootstrap-flat.js'></script>
<script>
delete $.fn.select2;
</script>
<script src='path/to/latest-select2.js'></script>
i already checked the $.fn.select2 by console.log and it shows that i'm now using the latest version.
I am seeing similar behavior. However for me it is only in chrome. I can also prevent it by scrolling such that I force the menu to be placed above the select box
I've found a solution: Just find this piece of code on select2.js:
`this.$results.on('mouseup',` '.select2-results__option[aria-selected]',
function (evt) {
var $this = $(this);
var data = $this.data('data');
if ($this.attr('aria-selected') === 'true') {
if (self.options.get('multiple')) {
self.trigger('unselect', {
originalEvent: evt,
data: data
});
} else {
self.trigger('close', {});
}
return;
}
self.trigger('select', {
originalEvent: evt,
data: data
});
});
`
And just comment this part of code on the 'mouseup' event handler and change it to 'click', like this:
`this.$results.on('click', '.select2-results__option[aria-selected]',
function (evt) {
var $this = $(this);
var data = $this.data('data');
//
// if ($this.attr('aria-selected') === 'true') {
// if (self.options.get('multiple')) {
// self.trigger('unselect', {
// originalEvent: evt,
// data: data
// });
// } else {
// self.trigger('close', {});
// }
//
// return;
// }
self.trigger('select', {
originalEvent: evt,
data: data
});
});`
My solution was to resolve some margin collapsing issues in the layout. There are some positioning issues in the dropdown if your body ends up having any margin to it.
Thanks @jpggvilaca ,
Your solution worked for me.
+1 in chrome, happened when hiding search box. Changing mousedown event to click worked for me
The issue with mousedown vs click is discussed more broadly in #4763, so please continue the conversation there.