title: JQuery Select Box and Loop Help
body: <p>Thanks for reading. I'm a bit new to jQuery, and am trying to make a script I can include in all my websites to solve a problem that always drives me crazy...</p>
  
  <p>The problem:
  Select boxes with long options get cut off in Internet Explorer. For example, these select boxes:
  <a href="http://discoverfire.com/test/select.php" rel="noreferrer">http://discoverfire.com/test/select.php</a></p>
  
  <p>In Firefox they are fine, but in IE, the options are cut off to the width of the select when they drop down.</p>
  
  <p>The solution:
  What I am looking to do, is create a script that I can include in any page that will do the following:</p>
  
  <ol>
  <li><p>Loop through all the selects on the page.</p></li>
  <li><p>For each select:
  A. Loop through its options.
  B. Find the width of the longest option.
  C. Bind a function to expand the select to that width on focus (or maybe click...).
  D. Bind a function to shrink to it's original width on blur.</p></li>
  </ol>
  
  <p>I've managed to do most of step #2 for one select box.</p>
  
  <p>I found that getting the options width was a problem (especially in IE), so I looped through and copied the text of each option to a span, measured the span width, and used the longest one as the width the select will be expanded to. Perhaps somebody has a better idea.</p>
  
  <p>Here is the code</p>
  
  <pre><code>&lt;script type='text/javascript'&gt;
  
        $(function() {
  
           /*
           This function will:
              1. Create a data store for the select called ResizeToWidth.
              2. Populate it with the width of the longest option, as approximated by span width.
  
           The data store can then be used
           */
           // Make a temporary span to hold the text of the options.
           $('body').append("&lt;span id='CurrentOptWidth'&gt;&lt;/span&gt;");
  
           $("#TheSelect option").each(function(i){
  
              // If this is the first time through, zero out ResizeToWidth (or it will end up NaN).
              if ( isNaN( $(this).parent().data('ResizeToWidth') ) ) {
                 $(this).parent().data( 'OriginalWidth', $(this).parent().width() );
                 $(this).parent().data('ResizeToWidth', 0);
  
                 $('CurrentOptWidth').css('font-family', $(this).css('font-family') );
                 $('CurrentOptWidth').css('font-size', $(this).css('font-size') );
                 $('CurrentOptWidth').css('font-weight', $(this).css('font-weight') );
  
              }
  
              // Put the text of the current option into the span.
              $('#CurrentOptWidth').text( $(this).text() );
  
              // Set ResizeToWidth to the longer of a) the current opt width, or b) itself. 
              //So it will hold the width of the longest option when we are done
              ResizeToWidth = Math.max( $('#CurrentOptWidth').width() , $(this).parent().data('ResizeToWidth') );
  
              // Update parent ResizeToWidth data.
              $(this).parent().data('ResizeToWidth', ResizeToWidth)
  
            });
  
           // Remove the temporary span.
           $('#CurrentOptWidth').remove();
  
           $('#TheSelect').focus(function(){
              $(this).width( $(this).data('ResizeToWidth') );
           });
  
           $('#TheSelect').blur(function(){
              $(this).width( $(this).data('OriginalWidth') );
           });
  
  
             alert( $('#TheSelect').data('OriginalWidth') );
             alert( $('#TheSelect').data('ResizeToWidth') );
  
        });
  
  
     &lt;/script&gt;
  </code></pre>
  
  <p>and the select:</p>
  
  <pre><code>&lt;select id='TheSelect' style='width:50px;'&gt;
     &lt;option value='1'&gt;One&lt;/option&gt;
     &lt;option value='2'&gt;Two&lt;/option&gt;
     &lt;option value='3'&gt;Three&lt;/option&gt;
     &lt;option value='42,693,748,756'&gt;Forty-two billion, six-hundred and ninety-three million, seven-hundred-forty-some-odd..... &lt;/option&gt;
     &lt;option value='5'&gt;Five&lt;/option&gt;
     &lt;option value='6'&gt;Six&lt;/option&gt;
     &lt;option value='7'&gt;Seven...&lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>Hopefully this will run for you if you want to run it, or you can see it in action here: <a href="http://discoverfire.com/test/select.php" rel="noreferrer">http://discoverfire.com/test/select.php</a>.</p>
  
  <p>What I need help with:
  This needs a bit of polish, but seems to work ok if you specify the select box.</p>
  
  <p>However, I don't seem to be able to figure out how to apply it to all select boxes on the page with a loop. So far, I have this:</p>
  
  <pre><code>$('select').each(
     function(i, select){
        // Get the options for the select here... can I use select.each...?
     }
  );
  </code></pre>
  
  <p>Also, is there a better way to get the length of the longest option for each select? The span is close, but not very exact. The problem is that IE returns zero for the option widths of the actual selects.</p>
  
  <p>Any ideas are very welcome, both for the questions asked, and any other improvements to my code.</p>
  
  <p>Thanks!!</p>