I have used Multiselect of Bootstrap.
My issue is when I select item from options. If name is too long then button width is also increased.
How do I handle Button width after selecting options.
I have used Multiselect of Bootstrap.
My issue is when I select item from options. If name is too long then button width is also increased.
How do I handle Button width after selecting options.
.multiselect
gives you a parameter called buttonWidth
. This will keep the width wider when nothing is selected as well. You can put a % in there as well. You can set the width like so:
<script type="text/javascript">
$(document).ready(function() {
$('#yourcontrol').multiselect({
buttonWidth: '100px'
});
//caret is in the middle, switch it to right
$(".caret").css('float', 'right');
$(".caret").css('margin', '8px 0');
});
</script>
Another work around is that in the "buttonText" function in the bootstrap-multiselect.js file, we can set the button text length and then return like this:
if(selected.length>20){
return selected.substr(0, 20); //if the text is too long, then this if statement will be executed
}
else{
return selected.substr(0, selected.length - this.delimiterText.length);
}
/*Bootstrap adds extra characters for the selected option.For example: the length of the option 'peter' will actually be 14 because of the delimiter and extra spaces. So you may want to set a bigger number in the if statement*/
Note: In the actual code, the return statement is like below:
return selected.substr(0, selected.length - this.delimiterText.length);
I had the same problem and I solved it a quicker way I believe. so you have your multiselect instantiation like this:
$('#anyID').multiselect({
and then some properties like
maxHeight:'300px'
well if you want to change button width upon a certain event. Do this:
buttonWidth:function(){
if(something happens)
return '300px'; // value chosen randomly
else
return "150px"; // value chosen randomly
},
and that way you would have
FINAL MULTISELECT
$('#anyID').multiselect({
maxHeight:'300px',
buttonWidth:function(){
if(something happens)
return '300px'; // value chosen randomly
else
return "150px"; // value chosen randomly
},
)};
Try using below code.
You can set max-width as you want in css.
Javascript:
CSS: