Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dynamically resize a select box...

Status
Not open for further replies.

netooio25

Programmer
Feb 20, 2006
6
US
I posted a similar question about this in the CSS forum because I was trying to find a css workaround for IE not supporting min-width but it looks like i'm going to have to use Javascript to do this.

I want to be able to dynamically resize the width of a select box. The select box has options added and removed dynamically already... I want the width of the select box to resize based on the width of the widest option inside it. Ex. When a really long option gets added, the select box should resize really wide to be able to show the entire option text. When a really long option gets deleted, the select box should resize back down to the width of the new widest text.

I am wondering if there is a way to do this through the DOM. I've been playing around with it and cant quite figure it out. I'll post my code from an example i was playing with. My idea was maybe checking the offsetWidth of the option elements or something like that and setting the selectbox width based on the width of the option elements.. However the option elements are showing as having zero width.

Also, dont think this will throw any kinks in it, but I want the select box to have a minimum-width it never resizes below, say 250px for example...

If this isnt the right way to go about it, any other ideas will be much appreciated too.


<html>
<head>
<link rel="stylesheet" type="text/css" href="kevin.css" title="Default">
<script>

// this was coming up as Zero
function getLongestOptionWidth() {
var selectElement = document.getElementById('1');
alert("Element 1 width = " + selectElement.offsetWidth);
selectElement = document.getElementById('2');
alert("Element 2 width = " + selectElement.clientWidth);
selectElement = document.getElementById('3');
alert("Element 3 width = " + selectElement.clientWidth);
selectElement = document.getElementById('4');
alert("Element 4 width = " + selectElement.clientWidth);
}


function addItem() {
var myoptions = document.getElementById('testid').options;
myoptions[myoptions.length] = new Option("This is a really really really asdfasdfasdfadsfdsafasdf really really long option text");
}

function deleteItem() {
var myoptions = document.getElementById('testid').options;
myoptions[myoptions.length-1] = null;
}

</script>

</head>

<body>

<div>
<select id="testid" multiple="true" size="10" style="overflow: scroll;" tabindex="18">
<OPTION id="1" VALUE="7382">steam turbine</OPTION>
<OPTION id="2" VALUE="2928">resistor array</OPTION>
<OPTION id="3" VALUE="3993">widget analyzer</OPTION>
<OPTION id="4" VALUE="9398">fiber identifier</OPTION>
</select>
</div>

<input type="submit" value="Add" onClick="addItem();">
<input type="submit" value="Delete" onClick="deleteItem();">
<input type="submit" value="GetLength" onClick="getLongestOptionWidth();">


</body>

</html>
 
I want the width of the select box to resize based on the width of the widest option inside it. Ex. When a really long option gets added, the select box should resize really wide to be able to show the entire option text.

<select> pulldowns have this behavior inherently unless you give them a specific width. If you are defining the width thru css then remove it and you should begin to see this behavior.

[small](on a side note I find that this is poor practice because often times other elements on the screen shift when the <select> box resizes - causing the page to jump around. IMO I think it is better to resize the <select> box to allow for the widest element that it *could* contain instead of what it *does* contain - that way the rest of the data on the page never shifts)[/small]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Yeah, I understand that. In the project i'm working on, I am defining a fixed width through css.... The reason I need to do this is initially, the select box is empty and if I dont define a fixed width it the select box is extremely narrow and this doesnt look very user appealing.

I need a minimum-width of about 250px to hold for the select box when there is nothing in it, or when the items in it are shorter than 250px. The only way I know to accomplish that is through declaring a fixed-width value in CSS, of course that keeps it from resizing for the larger items.

Also, for example, if I remove the fixed-width as you suggest... If you add an option that is, say 450 px long, it resizes the select box to 450 px or so to accomadate that. If you then remove that option, your left with a 450 px wide select box. I want it to resize back down to 250px(the minimum width) if the long option gets removed.

So basically right now i'm stuck.. If i define the fixed width, it doesnt resize for the larger options...If i dont define the fixed width, it doesnt hold the minimum-width or resize back down for shorter options if you delete a really wide option. This is why i'm trying to find a way to control this through Javascript.

Thanks for any help...
 
Heh, i didnt read the small text. I thought it was a signature.. I'm leaning towards doing that solution, just making it initially wide enough to hold the widest option.. but I'll have to change it every time a new option gets added to the program that is longer still.

Just wondering if its possible to control this with Javascript before heading that route.
 
[small]hi, my name is cory and I'm obsessed with kaht[/small]

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Just wondering if its possible to control this with Javascript before heading that route.

I don't think javascript can calculate a pixel width of text, so I would guess the answer is no. But I'm not 100% sure.

-kaht

[small] <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
[banghead] [small](He's back)[/small]
 
Does anyone know if an option inside a select box keeps track of its clientWidth or offsetWidth?

See my getLongestOptionWidth function in the code I included above for what I am trying to do...

A select element has a clientWidth that it keeps track of I believe, wondering if option elements keep track of the same property. I dont get any Javascript errors in the code above, just 0 for the options widths when i try to output them.

Thx,
Kevin
 
Can you get away with setting one option field of x spaces for the value so that it holds the size you need?
Then when you add new fields in you can always move the empty option to the bottom of the list and test to ensure it is never the selected option.

I wonder what happens if you use CSS to put an element that can grow in size inside an element with a fixed width. Would your select self-limit it's size within the fixed width element? I suspect it would just clip if it got too large depending on your overflow properties.


Stamp out, eliminate and abolish redundancy!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top