Due to a legacy front-end I got this hacky listener for opening my select2 (v4.0) dropdown upon focussing the select element. It works just fine, except for the fact that my tabindex is somehow set back to 1 when I press tab on the opened select2. How can I make it so that my dropdown closes and input#4 is focussed when I hit tab from within the opened dropdown?
var openedSelectEl;
// open select2 dropdown on focus
$(document).on('focus', 'select:enabled + span .select2-selection--single', function(e) {
openedSelectEl = $(this).parent().parent().siblings('select');
openedSelectEl.select2('open');
});
$("#3").select2({
data: [{
id: 'f00',
text: 'f00'
}, {
id: 'b4r',
text: 'b4r'
}, {
id: 'b4z',
text: 'b4z'
}]
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<div><input type="text" id="1" tabindex="1" /></div>
<div><input type="text" id="2" tabindex="2" /></div>
<div><select name="2" id="3" tabindex="3"></select></div>
<div><input type="text" id="4" tabindex="4"/></div>