Solved

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

Posted on 2008-06-11
7
906 Views
Last Modified: 2008-10-19
How do you Restrict width of a dropdown selectbox, but options are the full width of the Largest Option
0
Comment
Question by:jporter80
7 Comments
 
LVL 42

Expert Comment

by:David S.
ID: 21764984
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
 

Author Comment

by:jporter80
ID: 21764996
Thats why i need javascript, i believe you can do it with that but dont know how
0
 
LVL 29

Accepted Solution

by:
rdivilbiss earned 250 total points
ID: 21765409
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
 
LVL 15

Assisted Solution

by:mr_egyptian
mr_egyptian earned 250 total points
ID: 21766191
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
 
LVL 15

Expert Comment

by:mr_egyptian
ID: 21843171
Did any of these ideas work out for you jporter80?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
WordPress TK Title 8 24
Access Object's Property 9 22
Randomize in Owl Carousel v1.3.2 6 19
Session timeout 5 13
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now