Restrict width of a dropdown selectbox, but options are the full width of the Largest Option

How do you Restrict width of a dropdown selectbox, but options are the full width of the Largest Option
jporter80Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
rdivilbissConnect With a Mentor Commented:
I don't like the idea, but this seems to work or will at least get you started.
<html>
 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Expando</title>
<script type="text/javascript">
<!--
function expando(obj) {
	obj.style.width='150px';
	
}
 
function change(obj) {
	obj.style.width='80px';
	document.getElementById('nextField').focus();
}
 
function shrink(obj) {
	obj.style.width='80px';
}
//-->
</script>
</head>
 
<body>
 
<p><select size="1" id="select2" name="select2" style="width: 80px" onclick="expando(this);" onchange="change(this);" onblur="shrink(this);">
<option value="Mississippi">Mississippi</option>
<option value="Ak">Ak</option>
 
<option value="Gobbledygop">Gobbledygoop</option>
</select>&nbsp; <input id="nextField" type="text" name="T1" size="20" onfocus="shrink(document.getElementById('select2'));)"></p>
 
</body>
 
</html>

Open in new window

0
 
David S.Commented:
You can use CSS to control the width, but then any text that is too wide will not be visible at all, so I don't recommend doing it.
0
 
jporter80Author Commented:
Thats why i need javascript, i believe you can do it with that but dont know how
0
 
mr_egyptianConnect With a Mentor Commented:
It goes without saying that IE sucks balls, but I'll say it anyhow.  You can probably put something together with one of the popular javascript libraries, like ext:

http://extjs.com/

I spent a little time and threw a fake select menu together.  Far from perfect, but you're welcome to it if you can use it, and want to take the time to improve it.:

http://serial.heinous.net/~brut/drop2.html

I think you may just need to rethink your design unfortunately, or not rely on the select form element.

If the code that rdivilbiss posted above, will work for you I've attached a single function below to do the trick without hardcoding any values.  Just set the width:

<select style="width: 5em;" onclick="selwidth(this);" onblur="selwidth(this):">

Good luck...
  var originalwidth=null;
  
  function selwidth(obj){
	  if(originalwidth==null){
		  originalwidth=obj.style.width;
		  obj.style.width="auto";
	  }else{
		  obj.style.width=originalwidth;
		  originalwidth=null;
	  }
  }

Open in new window

0
 
mr_egyptianCommented:
Did any of these ideas work out for you jporter80?
0
All Courses

From novice to tech pro — start learning today.