File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes Dynamic width of s:select Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "Dynamic width of s:select" Watch "Dynamic width of s:select" New topic
Author

Dynamic width of s:select

Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 182
Hi,

Can I make an s:select list to have a static width to have the following properties

1. When the dropdown is selected, it dynamically increases based on the largest text in the list
2. When selected the list goes back to the static width.

Thanks !!
JD
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2551
    
    2

"J D " please check your private messages for an important administrative matter. You can see them by clicking the My Private Messages link above.


SCJP, SCWCD.
|Asking Good Questions|
Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 182
Hi,

To be a bit more descriptive -
<td>
<s:select name="" label="" theme="simple" list="" cssStyle="width:20px;"
value="" id="">
</s:select>
</td>

So as you see it has a standard width but can I get the select list to increase width based when I click on the drop down.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56185
    
  13

What is <s:select>? It's not a standard tag.

In any case, you can only affect the <select> element via it's HTML attributes and CSS, so this has been moved to the HTML forum.
[ December 07, 2008: Message edited by: Bear Bibeault ]

[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 182
taglib prefix="s" uri="/struts-tags"

I tried the following javascript but it does not reset the width to 20 px once the combo is set
function autoWidth()
{
var maxlength = 0;
var mySelect = document.getElementById('test');
for (var i=0; i<mySelect.options.length;i++)
{
if (mySelect[i].text.length>maxlength)
{
maxlength = mySelect[i].text.length;
}
}
mySelect.style.width = maxlength * 10;
}

<s:select list="" cssStyle="width:20px;" id="test" onKlick="autoWidth();">
</s:select>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 56185
    
  13

Please be sure to use UBB code tags when posting code to the forums. Unformatted code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please read this for more information.

You can go back and change your post to add code tags by clicking the .
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
I think width of the select tag depends mostly on the length of the text in your options list
Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 182
Hi Sri Anand -

That is what happens but the length goes all the way to 100 characters and I don't want a list that wide. Whereas if I set the width to 20 (which is all the space I have) the combo box gets clipped of.

I remember seeing websites where the dropdown is wider and then when the selection is made it narrows down but I don't seem to be lucky enough to find one now.

I even played around with trying to get a dynamic tooltip that would display the whole text, but that was a dead end as far as I know.
Sri Anand
Ranch Hand

Joined: Mar 06, 2005
Posts: 392
I had a similar issue i could not get above this problem,
Issue
display seven long meesages in drop down and user needs to select one of them there were lot of fields in teh form and page lay out was messing up because of length of select
solution or work around
i numbered each message and below the select, i put a multi line text box and when ever a particular number is selected in select, the corresponding message of the number is displayed in the text box, thus keeping the size of the select small

its kind of work around i did to get above this problem if you are sucessful in finding better solution let me know
Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 182
Hey !!

Just what I have started with... I don't know of a better workaround, all that I need to make up my mind on now is whether, to have text in a div as label or have a textarea...

I am hoping to find a better solution...

Thanks for your inputs !!
Jay Abrahm
Ranch Hand

Joined: May 28, 2008
Posts: 182
Hi,

This issue is now resolved. I had posted my issue in http://www.webdeveloper.com/ to get the solution.

Facun Fernetn
Greenhorn

Joined: Apr 22, 2009
Posts: 1
Hi Jay Abrahm!
Thanks for the source code you post above. I've been testing it and I found that this doesn't work in IE7 or IE8, but it works fine in Firefox, do you know why ? I need this solution for IE7 or IE8...
Thanks in advance ! Regards !
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Dynamic width of s:select
 
Similar Threads
<s:select in <s:iterate tag displaying with comma seperated value
Retaining the value in struts2 using Select and optionTransferSelect
Need help regarding s:select tag list
how to make a option selected in <s:select>
Highlighting Multiple values in a Multi-Select dropdown using Struts2