Home > Net >  jquery select 2 change dropdown width dynamically
jquery select 2 change dropdown width dynamically

Time:02-18

I'm using jquery select 2 and need to reduce width of dropdown dynamically.

This is my code,

$('.myselect').select2();

$('.myselect').on('select2:open', function(e) {
       var width = $('.select2-dropdown').width();
       width -= 20;
       $('.select2-dropdown').attr('style', 'width: ' width 'px !important');
});

What I need to do is reduce width of dropdown by 20px. But tis code not working

CodePudding user response:

Just wait 1ms :D And it's probably select2 event bug. http://jsfiddle.net/4f3td9eL/

setTimeout(function(){
    //code that's interacting with the dropdown
}, 1);
  • Related