select2 icon indicating copy to clipboard operation
select2 copied to clipboard

select2 weird behavior upon clicking

Open ejlocop opened this issue 10 years ago • 6 comments

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.

ejlocop avatar Feb 22 '16 05:02 ejlocop

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

joshcutler avatar Feb 23 '16 21:02 joshcutler

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
  });
});`

jpggvilaca avatar Feb 25 '16 16:02 jpggvilaca

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.

joshcutler avatar Feb 25 '16 19:02 joshcutler

Thanks @jpggvilaca ,

Your solution worked for me.

lukasjuhas avatar Mar 02 '16 15:03 lukasjuhas

+1 in chrome, happened when hiding search box. Changing mousedown event to click worked for me

fanals avatar Aug 19 '16 12:08 fanals

The issue with mousedown vs click is discussed more broadly in #4763, so please continue the conversation there.

alexweissman avatar Nov 06 '17 02:11 alexweissman