7

i would like to use jquery ui selectmenu from felixnagel. it works but i have the problem that jquery always set a too small width for the selectmenu so that the open/close icon appears above the content. I know that i can set the width in the command selectmenu() but i have a couple of different selectmenus, so i would like to know where i can take influence on the correct calculation how wide the selectmenu will be, so that i can give it an additional px for the width.

I tried to find it in the .js files, but since now i was not successfull with it. Hopefully someone of you have an idea what i can do here.

thank you very much

Ruven

Ruven JR. Maerson
  • 309
  • 1
  • 6
  • 16

5 Answers5

11

This is simple and makes it adjust based on content.

$( "select" ).selectmenu({ width : 'auto'});


This uses a hard coded width to avoid the problem when selecting items of different widths mentioned in the comment:

$( "select" ).selectmenu({ width : 250});
Martin
  • 5,165
  • 1
  • 37
  • 50
Kinoli
  • 139
  • 1
  • 4
  • 3
    Problem with this option is that it adjusts the width of the select to whatever is currently selected.. so it keeps changing width and breaks the UI a bit by having the edges of the options window not matching up to the select.. – Robert Mark Bram May 09 '15 at 05:58
  • @Robert: I used Kinolis answer but used a constant value for the with: So you can just use `$( "select" ).selectmenu({ width : 250});` for example. – Martin Jul 15 '16 at 10:46
9

Maybe something like this will help you

<select width="200">
    <option>Small</option>
</select>


$(document).ready(function(){
    $.each($('select'), function () {
        $(this).selectmenu({ width : $(this).attr("width")})
    })
})

You walk thru every select element and you will have the possibilty to specify the with in the select elements width attribute. Maybe you find it useful and can modify it so it fill your needs.

Edit 1: Or if you just want to add something extra thru code (I think you can do it also with CSS) you could use something like:

<select>
    <option>Small</option>
</select>


$(document).ready(function(){
    $.each($('select'), function () {
        $(this).selectmenu({ width : $(this).width() + 100}) // You just take the width of the current select and add some extra value to it
    })
})
Simon Edström
  • 6,461
  • 7
  • 32
  • 52
  • Oh man. Sadly i did´nt had this great idea. Thank you very much for your quick help. I thought of sth. similar, but prefered to directly manipulate the initialization. Anyway sometimes i should better choose the quick and "dirty" was. Thank you. – Ruven JR. Maerson Feb 08 '12 at 20:01
1

It's kind of a hack and I don't know how robust this is but I've successfully used this in one particular instance:

$("select").each(function(){
    var $this=$(this);
    $this.selectmenu();
    var menu=$this.selectmenu("menuWidget");
    var button=$this.selectmenu("widget");
    button.click();
    var longest=null;
    menu.children("li").each(function(){
        var $this=$(this);
        if(longest==null || longest.text().length < $this.text().length){
            longest=$this;
        }
    });
    menu.css("width","auto");
    var width=longest.mouseover().width();
    button.click();
    var innerButton = button.find(".ui-selectmenu-text");
    var paddingRight=parseFloat(innerButton.css("padding-right"));
    var paddingLeft=parseFloat(innerButton.css("padding-left"));
    var total=width+paddingLeft+paddingRight + 2;
    $this.selectmenu("option","width", total);
})

In a nutshell what it does is:

  • open the menu so that it is filled and laid out
  • find its longest element
  • set the menu width to auto, hover over the longest element and get its width
  • close the menu
  • get the left and right padding of the button widget
  • add that to the computed width of the longest item and use that as the width
    • adding 2 pixels for good measure (I think it's borders or something)

It's just that it kind of heavily relies on how jquery-ui does things internally so it might break upon any update.

See this fiddle: http://jsfiddle.net/txo1mvhn/2/

Tuan-Tu
  • 73
  • 5
0

Previous solutions were almost right.

You have to set css width and call selectmenu like this:

$('select').selectmenu({ width : $(this).css('width')});
John Palmer
  • 25,356
  • 3
  • 48
  • 67
  • 1
    $('select').selectmenu({ width : $(this).css('width')}); Uncaught TypeError: Cannot use 'in' operator to search for 'display' in undefined – Robert Mark Bram May 09 '15 at 06:02
0

For percentages, is like this:

Declare the percentage in your CSS:

 select{
    width: 100%;
 }

And in your JavaScript:

jQuery("select").selectmenu(
        {
            width:  jQuery(this).attr("width"),
        }   
);
  • this answer is incorrect, jQuery.attr() function is for **element attributes** not for css properties. unfortunately, there is no easy way to get percentage size from css. look for this question: http://stackoverflow.com/questions/744319/get-css-rules-percentage-value-in-jquery idea: you might create function to get elements relative width and define your width like this: width: function() { get_element_relative_width(this) } – Radomír Laučík Aug 28 '15 at 07:38