1

i have applied select2 on some select box, but i want to open these only on left click, not on right or middle click.

jQuery(".dropdown").select2({placeholder: "some text"});

I tried many things but nothing seems to work. please help.

Aniket Singh
  • 2,464
  • 1
  • 21
  • 32

1 Answers1

1

Looking at the following question on stack: How to distinguish between left and right mouse click with jQuery

You could use a switch case to keep the select element closed depending on which mouse button was clicked, example:

$('.select2-selection').mousedown(function(event) {
    switch (event.which) {
        case 1:
            alert('Left Mouse button pressed.');
            break;
        case 2:
            alert('Middle Mouse button pressed.');
            $('select').select2("close"); 
            break;
        case 3:
            alert('Right Mouse button pressed.');
            $('select').select2("close"); 
            break;
        default:
            alert('You have a strange Mouse!');
    }
});

Alternatively you could use the disable/enable options in Select2.

Community
  • 1
  • 1
James
  • 442
  • 2
  • 13
  • my problem is not to recognize mouse buttons only, i want wanted to open select2 box only on left click... i tried this already. btw thnx. – Aniket Singh Aug 04 '15 at 10:33
  • Select 2 has a close method you can use. So you could just close the select box using that method depending on which button was used? I've edited my answer to reflect this. I appreciate his doesn't directly answer your question but it may help. – James Aug 04 '15 at 10:44
  • thnx.. but i used `$(select2_obj).select2("close"); ` beacuse inside `mousedown` function `this` indicating object of class `select2-selection`.. Thnk u once again. – Aniket Singh Aug 04 '15 at 11:13
  • You're welcome - it's not the most elegant solution. If it's solved the problem, could you please accept the answer. Thanks. – James Aug 04 '15 at 11:32