views:

951

answers:

1

So I've got some <select> tags that I would like to show the entire contents in IE. I've looked around and found a few fixes, but I don't want to include YUI since I'm already using jQuery elsewhere, and would prefer to keep the selects on the page instead of replacing it with DIVs.

In the code that I've come up with, FF3 works great. In Internet Explorer (6,7,8) the first click on the <select> flashes the drop down and then it disappears. I've tried focus and mousedown in place of the click event in the code, no success.

If I remove both of the setting of element.css in autoWidth the drop downs work as expected, without the bonus of having a nice width. Does anyone know what would cause the setting of the css to make the drop down fail in IE?

<head>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;&lt;/script&gt; 
 <script type="text/javascript">
 var count = 0;
 autoWidth = function(e) {
  $element = $(e.target)
  $element.css("width","auto");
  if($element.width() < $element.data("originalWidth"))
  {
   $element.css("width", $element.data("originalCSSWidth"));
  }
 }
 resetWidth = function(e) {
  $element = $(e.target)
  $element.css("width", $element.data("originalCSSWidth"));
  $("#counter").text(++count);
 }
 recordEvent = function(e) {
  $("#event").text($("#event").text() + " " + e.type);
 }
 dropDownIEWidthFix = function() {
 //if($.browser.msie)
 //{
  $dropDown = $(this);
  $dropDown.data("originalWidth", $dropDown.width());
  $dropDown.data("originalCSSWidth", $dropDown.css("width"));
  $dropDown.blur(recordEvent);
  $dropDown.blur(resetWidth);
  $dropDown.change(recordEvent);
  $dropDown.change(resetWidth);
  $dropDown.click(recordEvent);
  $dropDown.click(autoWidth);
 //}
 };
 $(function() {
  $("select.officeItemList").each(dropDownIEWidthFix);
 });
 function trackingSelectionChanged(select)
 {
  $("#event").text($("#event").text() + " inlineOnChange");
 }
 </script>
</head>
<body>
 <div style="overflow:hidden;width:148px;">
  <select class="officeItemList" style="width:148px;" onchange="trackingSelectionChanged(this);">
   <option value="1">erasersdffffffffffeeefefsdfsdfsdfsdfsdfsdfsdfsdfsdfdsfsdfefesf</option>
   <option value="3">pencil</option>
   <option value="4">ruler</option>
   <option value="5">ink</option>
   <option value="7">A4 paper</option>
   <option value="8">A3 paper</option>
   <option value="9">erasersdffffffffffeeefefsdfsdfsdfsdfsdfsdfsdfsdfsdfdsfsdfefesf</option>
   <option value="10">executive</option>
   <option value="11">janitor</option>
   <option value="12">developer</option>
  </select>
 </div>
 <div style="overflow:hidden;width:160px;">
  <select class="officeItemList" style="width:160px;" onchange="trackingSelectionChanged(this);">
   <option value="3">pencil</option>
   <option value="4">ruler</option>
   <option value="5">ink</option>
   <option value="7">A4 paper</option>
   <option value="8">A3 paper</option>
   <option value="9">erasersdffffffffffeeefefsdfsdfsdfsdfsdfsdfsdfsdfsdfdsfsdfefesf</option>
   <option value="10">executive</option>
   <option value="11">janitor</option>
   <option value="12">developer</option>
  </select>
 </div>
 <div style="overflow:hidden;width:160px;">
  <select class="officeItemList" style="width:160px;" onchange="trackingSelectionChanged(this);">
   <option value="1">eee</option>
   <option value="3">pencil</option>
   <option value="4">ruler</option>
   <option value="5">ink</option>
   <option value="7">A4 paper</option>
   <option value="8">A3 paper</option>
   <option value="9">ffff</option>
   <option value="10">executive</option>
   <option value="11">janitor</option>
  </select>
 </div>
 <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
 AdjustWidth fired :<span id="counter"></span> times
 <div id="event"></div>
</body>
+1  A: 

Looks like the problem is with IE's rendering of the drop downs. If you change the css on the <select> the drop down gets reset (the flicker on initial click).

So, using the mousedown event instead of click fixes the first two drop downs, but because of the additional reset on the third drop down (since it is large enough already) breaks IE's native change event (the drop down value never changes regardless of what options you click).

Solution: On the initial page load - don't bind the events if the drop down is large enough already. I switched the width to auto, and measured the <select>, then only bind the width change events when the drop downs need it, you can avoid the events to the other drop downs.

Here is the code- tested IE 6,7,8. FireFox works correctly to begin with, same with Safari. You can see a line commented with IE6 fix, this line, when removed, causes the original (incorrect) behavior in IE6. To be honest, I'm not sure why.

<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"&gt;&lt;/script&gt; 
     <script type="text/javascript">
dropDownIEWidthFix = function() {
if($.browser.msie) {
 $dropDown = $(this);
 $dropDown.data("originalWidth", $dropDown.width());
 $dropDown.data("originalCSSWidth", $dropDown.css("width"));
 $dropDown.css("width", "auto");
 if($dropDown.width() > $dropDown.data("originalWidth")) {
  $dropDown.blur(function(e) {
   $element = $(e.target);
   $element.css("width", $element.data("originalCSSWidth"));
  });
  $dropDown.change(function(e) {
   $element = $(e.target);
   $element.css("width", $element.data("originalCSSWidth"));
  });
  $dropDown.mousedown(function(e) {
   $element = $(e.target);
   $element.css("width","auto");
   $element.width(); //IE6 fix.
  });
 }
 $dropDown.css("width", $dropDown.data("originalCSSWidth"));
}}
$(function() {
 $("select.officeItemList").each(dropDownIEWidthFix);
});
function trackingSelectionChanged(select)
{
}
</script>
</head>
<body>
    <div style="float: left; display: inline; width: 160px;">
     <div class="FUNCOptionTitle">
      <span style="text-align: right;">col1</span>
     </div>
     <div style="overflow:hidden;width:148px;">
      <select class="officeItemList" style="width:148px;" onchange="trackingSelectionChanged(this);">
       <option value="1">erasersdffffffffffeeefefsdfsdfsdfsdfsdfsdfsdfsdfsdfdsfsdfefesf</option>
       <option value="3">pencil</option>
       <option value="4">ruler</option>
       <option value="5">ink</option>
       <option value="7">A4 paper</option>
       <option value="8">A3 paper</option>
       <option value="9">erasersdffffffffffeeefefsdfsdfsdfsdfsdfsdfsdfsdfsdfdsfsdfefesf</option>
       <option value="10">executive</option>
       <option value="11">janitor</option>
       <option value="12">developer</option>
      </select>
     </div>
    </div>
    <div style="float: left; display: inline; width: 165px;">
     <div class="FUNCOptionTitle">
      <span style="text-align: right;">col2</span>
     </div>
     <div style="overflow:hidden;width:160px;">
      <select class="officeItemList" style="width:160px;" onchange="trackingSelectionChanged(this);">
       <option value="3">pencil</option>
       <option value="4">ruler</option>
       <option value="5">ink</option>
       <option value="7">A4 paper</option>
       <option value="8">A3 paper</option>
       <option value="9">erasersdffffffffffeeefefsdfsdfsdfsdfsdfsdfsdfsdfsdfdsfsdfefesf</option>
       <option value="10">executive</option>
       <option value="11">janitor</option>
       <option value="12">developer</option>
      </select>
     </div>
    </div>
    <div style="float: left; display: inline; width: 165px;">
     <div class="FUNCOptionTitle">
      <span style="text-align: right;">col3</span>
      </div>
     <div style="overflow:hidden;width:160px;">
      <select class="officeItemList" style="width:160px;" onchange="trackingSelectionChanged(this);">
       <option value="1">eee</option>
       <option value="3">pencil</option>
       <option value="4">ruler</option>
       <option value="5">ink</option>
       <option value="7">A4 paper</option>
       <option value="8">A3 paper</option>
       <option value="9">ffff</option>
       <option value="10">executive</option>
       <option value="11">janitor</option>
      </select>
     </div>
    </div>
    <br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br />
    AdjustWidth fired :<span id="counter"></span> times
    <div id="event"></div>
</body>
s_hewitt