Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Select using Javascript/jQuery?

Is there a way to open a select box using Javascript (and jQuery)?

<select style="width:150px;">     <option value="1">1</option>     <option value="2">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nunc arcu nunc, rhoncus ac dignissim at, rhoncus ac tellus.</option>     <option value="3">3</option> </select> 

I have to open my select, cause of IE bug. All versions of IE (6,7,8) cut my options. As far as I know, there is no CSS bugfix for this. At the moment I try to do the following:

var original_width = 0; var selected_val = false;  if (jQuery.browser.msie) {     $('select').click(function(){         if (selected_val == false){             if(original_width == 0)                 original_width = $(this).width();              $(this).css({                 'position' : 'absolute',                 'width' : 'auto'             });         }else{             $(this).css({                 'position' : 'relative',                 'width' : original_width             });             selected_val = false;         }     });      $('select').blur(function(){         $(this).css({             'position' : 'relative',             'width' : original_width         });     });      $('select').blur(function(){         $(this).css({             'position' : 'relative',             'width' : original_width         });     });                  $('select').change(function(){         $(this).css({             'position' : 'relative',             'width' : original_width         });     });      $('select option').click(function(){         $(this).css({             'position' : 'relative',             'width' : original_width         });              selected_val = true;     });  } 

But clicking on my select the first time will change the width of the select but I have to click again to open it.

like image 719
Newbie Avatar asked Jan 12 '10 10:01

Newbie


1 Answers

I know this is pretty old and answered, but this worked for me in Safari and iOS UIWebView - I have it hidden, but want it to show and open when a different button is clicked.

$('#select-id').show().focus().click(); 
like image 61
Matt H Avatar answered Sep 28 '22 10:09

Matt H