code_junkie

How to expand 'select' option width after the user wants to select an option

Nov 12th, 2011
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.71 KB | None | 0 0
  1. <select style="width: 120px">
  2. <option>ABC</option>
  3. <option>REALLY LONG TEXT, REALLY LONG TEXT, REALLY LONG TEXT</option>
  4. </select>
  5.  
  6. <div style="width: 180px; overflow: hidden;">
  7. <select style="width: auto;" name="abc" id="10">
  8. <option value="-1">Any</option>
  9. <option value="123">123</option>
  10. </select>
  11. </div>
  12.  
  13. $(document).ready( function() {
  14. $('#select').change( function() {
  15. $('#hiddenDiv').html( $('#select').val() );
  16. $('#select').width( $('#hiddenDiv').width() );
  17. }
  18. }
  19.  
  20. <div id="hiddenDiv" style="visibility:hidden"></div>
  21.  
  22. <html>
  23. <head>
  24.  
  25. <style>
  26. .wrapper{
  27. display: inline;
  28. float: left;
  29. width: 180px;
  30. overflow: hidden;
  31. }
  32. .selectArrow{
  33. display: inline;
  34. float: left;
  35. width: 17px;
  36. height: 20px;
  37. border:1px solid #7f9db9;
  38. border-left: none;
  39. background: url('selectArrow.png') no-repeat 1px 1px;
  40. }
  41. .selectArrow-mousedown{background: url('selectArrow-mousedown.png') no-repeat 1px 1px;}
  42. .selectArrow-mouseover{background: url('selectArrow-mouseover.png') no-repeat 1px 1px;}
  43. </style>
  44. <script language="javascript" src="jquery-1.3.2.min.js"></script>
  45.  
  46. <script language="javascript">
  47. $(document).ready(function(){
  48. $('#w1').wrap("<div class='wrapper'></div>");
  49. $('.wrapper').after("<div class='selectArrow'/>");
  50. $('.wrapper').find('select').mousedown(function(){
  51. $(this).parent().next().addClass('selectArrow-mousedown').removeClass('selectArrow-mouseover');
  52. }).
  53. mouseup(function(){
  54. $(this).parent().next().removeClass('selectArrow-mousedown').addClass('selectArrow-mouseover');
  55. }).
  56. hover(function(){
  57. $(this).parent().next().addClass('selectArrow-mouseover');
  58. }, function(){
  59. $(this).parent().next().removeClass('selectArrow-mouseover');
  60. });
  61.  
  62. $('.selectArrow').click(function(){
  63. $(this).prev().find('select').focus();
  64. });
  65.  
  66. $('.selectArrow').mousedown(function(){
  67. $(this).addClass('selectArrow-mousedown').removeClass('selectArrow-mouseover');
  68. }).
  69. mouseup(function(){
  70. $(this).removeClass('selectArrow-mousedown').addClass('selectArrow-mouseover');
  71. }).
  72. hover(function(){
  73. $(this).addClass('selectArrow-mouseover');
  74. }, function(){
  75. $(this).removeClass('selectArrow-mouseover');
  76. });
  77. });
  78.  
  79. </script>
  80. </head>
  81. <body>
  82. <select id="w1">
  83. <option value="0">AnyAnyAnyAnyAnyAnyAny</option>
  84. <option value="1">AnyAnyAnyAnyAnyAnyAnyAnyAnyAnyAnyAnyAnyAny</option>
  85. </select>
  86.  
  87. </body>
  88. </html>
  89.  
  90. if (jQuery.browser.msie) {
  91. jQuery('#mySelect').focus(function() {
  92. jQuery(this).width('auto');
  93. }).bind('blur change', function() {
  94. jQuery(this).width('100%');
  95. });
  96. };
  97.  
  98. <!--
  99.  
  100. I found this works fairly well.
  101.  
  102. -->
  103.  
  104. <!-- On page load, be sure that something else has focus. -->
  105. <body onload="document.getElementById('name').focus();">
  106. <input id=name type=text>
  107.  
  108. <!-- This div is for demonstration only. The parent container may be anything -->
  109. <div style="height:50; width:100px; border:1px solid red;">
  110.  
  111. <!-- Note: static width, absolute position but no top or left specified, Z-Index +1 -->
  112. <select
  113. style="width:96px; position:absolute; z-index:+1;"
  114. onactivate="this.style.width='auto';"
  115. onchange="this.blur();"
  116. onblur="this.style.width='96px';">
  117. <!-- "activate" happens before all else and "width='auto'" expands per content -->
  118. <!-- Both making a selection and moving to another control should return static width -->
  119.  
  120. <option>abc</option>
  121. <option>abcdefghij</option>
  122. <option>abcdefghijklmnop</option>
  123. <option>abcdefghijklmnopqrstuvwxyz</option>
  124.  
  125. </select>
  126.  
  127. </div>
  128.  
  129. </body>
  130.  
  131. </html>
  132.  
  133. <div id=myForm>
  134.  
  135. #myForm select {
  136. width:200px; }
  137.  
  138. #myForm select:focus {
  139. width:auto; }
Add Comment
Please, Sign In to add comment