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!

How to limit the width of a SELECT option list? 1

Status
Not open for further replies.

dbMark

Programmer
Apr 10, 2003
1,515
US
I have created a SELECT list wherein multiple options can be selected. I have it placed within a table cell. Everything works fine except it is just too wide for the space I have allowed for it. This is due to there being long text displayed in some of the individual OPTION items. So the entire list is as wide as the longest line. but I must keep the exact text, so I can't shorten them. How can I force it to be smaller? I'd like to avoid changing the font, so I tried forcing the displayable text onto 2 lines with < BR > but apparently the list won't allow more than one display line per item. Also I tried limiting the width of the table cell it was in, but I failed there too. Would something in CSS do it? Thanks for your help.
 
[tt]select style=&quot;width:100px&quot;>[/tt]

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
Dwarfthrower, I had placed the limit on the table cell, not the select. You had it right, but now I cannot see the entire line since it does not word-wrap. Any hope for that? Or else I'll have to change the specs, or shrink the font, both choices I'd rather avoid...

By the way, aren't you ever offline?
 
Hmmm... Wrapping the text in an option is just one of those things that for some reason, you just can not do. Probably stems from the fact that select controls are handled differently to other HTML form controls.

The only way around it that I could see was to build a DHTML widget that looked and acted like a select control, but was actually just a series of divs that passed values back to a standard input control (text or hidden).

The sample below demonstrates what I'm getting at:
Code:
<html>
<head>
<style type=&quot;text/css&quot;>
.DHTML_Select_Control{
 width: 150px;
 height: 100px;
 overflow-x: none;
 overflow-y: auto;
 border-left: 3px inset #DDDDDD;
 border-top: 3px inset #DDDDDD;
 border-right:  3px inset #EEEEEE;
 border-bottom:  3px inset #EEEEEE;
}
.DHTML_Select_Option{
 font-family: Helvetica;
 font-size: smaller;
 color: #000000;
}
.DHTML_Select_Option_Selected{
 font-family: Helvetica;
 font-size: smaller;
 color: #FFFFFF;
 background: #000066;
}
.DHTML_Select_Option_Clicked{
 font-family: Helvetica;
 font-size: smaller;
 color: #FFFFFF;
 background: #000066;
 border: 1px dotted #FFFFFF;
}
</style>
<script type=&quot;text/javascript&quot; language=&quot;JavaScript&quot;>
//document.onselectstart=new Function (&quot;return false&quot;);
var option_last_clicked;
function handleSelectClick(objClickedOn, strHiddenFieldID){
 event.cancelBubble = true;
 var objTheOption = event.srcElement;
 if(objTheOption != objClickedOn){
  var strTheOptionValue = objTheOption.value;
  if(!event.ctrlKey){
   if(!event.shiftKey){
    //neither shift or ctrl were held down during the click
    for(var i = 0; i < objClickedOn.childNodes.length; i++){
     var currNode = objClickedOn.childNodes.item(i); 
      currNode.className = 'DHTML_Select_Option';
    }
    objTheOption.className = 'DHTML_Select_Option_Clicked';
    option_last_clicked = objTheOption;
   }
   else{
    //only shift key present
    for(var i = 0; i < objClickedOn.childNodes.length; i++){
     var currNode = objClickedOn.childNodes.item(i); 
      currNode.className = 'DHTML_Select_Option';
    }
    var lastClickedOnPos = option_last_clicked.sourceIndex;
    var thisClickedOnPos = objTheOption.sourceIndex;
    if(lastClickedOnPos < thisClickedOnPos){
     for(var i = lastClickedOnPos; i <= thisClickedOnPos; i++){
      document.all[i].className = 'DHTML_Select_Option_Selected';
     }
    }
    else{
     for(var i = lastClickedOnPos; i <= thisClickedOnPos; i++){
      document.all[i].className = 'DHTML_Select_Option_Selected';
     }
    } 
    objTheOption.className = 'DHTML_Select_Option_Clicked';
    option_last_clicked = objTheOption;
   }
  }
  else{
   // only ctrl key present or ctrl and shift both present, ctrl to take precedence
   option_last_clicked.className = 'DHTML_Select_Option_Selected';
   objTheOption.className = 'DHTML_Select_Option_Clicked';
   option_last_clicked = objTheOption;
  }
 }
 //compile the value and write it out to the hidden field
 var hidField = document.getElementById(strHiddenFieldID);
 var strValueList = ''
 for(var i = 0; i < objClickedOn.childNodes.length; i++){
  var currNode = objClickedOn.childNodes.item(i); 
  if(currNode.className != 'DHTML_Select_Option'){
   if(strValueList == ''){
    strValueList = currNode.value;
   }
   else{
    strValueList = strValueList + ';' + currNode.value;
   }
  }
 }
 hidField.value = strValueList;
}
</script> 
</head>
<body>
<div class=&quot;DHTML_Select_Control&quot; onselectstart=&quot;return false;&quot; onclick=&quot;handleSelectClick(this, 'myhiddenfield'); return false;	&quot;>
<div class=&quot;DHTML_Select_Option&quot; value=&quot;Option 1 value&quot;>Option 1 text is quite long</div>
<div class=&quot;DHTML_Select_Option&quot; value=&quot;Option 2 value&quot;>Option 2 text is a little bit longer</div>
<div class=&quot;DHTML_Select_Option&quot; value=&quot;Option 3 value&quot;>Option 3 text is really really really really long</div>
<div class=&quot;DHTML_Select_Option&quot; value=&quot;Option 4 value&quot;>Option 4 text is absolutely, positively, without a doubt, the longest option here</div>
<div class=&quot;DHTML_Select_Option&quot; value=&quot;Option 5 value&quot;>short option</div>
</div>
<!-- Change this to a hidden field or put it in a hidden div tag -->
<input type=&quot;text&quot; id=&quot;myhiddenfield&quot; value=&quot;&quot;>
</body>
</html>

* Tested with IE6 under Win2K

Never be afraid to share your dreams with the world.
There's nothing the world loves more than the taste of really sweet dreams.
 
I was afraid that SELECT was locked into single line options. I consider that a definite limitation to otherwise flexible HTML! Thanks for the alternative. I'll examine it closely.
 
Well, isn't the select widget as a rule a one-liner? Even in VB or Delphi, it's a one-liner. The flexible part is where it expands to cover whatever crazy text is in there (which begs the suggestion that you be a little more efficient at choosing the text that goes in).

Another option is temporarily changing the width of the drop-down. Like this:

Code:
<!DOCTYPE html 
     PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;
     &quot;DTD/xhtml1-transitional.dtd&quot;>
<html>
  <head>
    <meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot;></meta>
    <title>Sample File</title>
  </head>
  <body>
    <p>Here are a variety of drop-downs.  Click on each one to see your options:</p>
    <form name=&quot;LilFormie&quot;>
      <select name=&quot;Color1&quot; id=&quot;Color1&quot; width=&quot;60&quot; style=&quot;width: 60px&quot; onfocus=&quot;document.LilFormie.Color1.style.width='200px';return false;&quot; onblur=&quot;document.LilFormie.Color1.style.width='60px';return false;&quot;>
        <option value=&quot;red&quot;>red</option>
        <option value=&quot;orange&quot;>orange</option>
        <option value=&quot;yellow&quot;>yellow</option>
        <option value=&quot;green&quot;>green</option>
        <option value=&quot;blue&quot;>blue</option>
        <option value=&quot;indigo&quot;>indigo</option>
        <option value=&quot;violet&quot;>violet</option>
        <option value=&quot;feet&quot;>the same color as my feet</option>
      </select>
      <select name=&quot;Color2&quot; id=&quot;Color2&quot; width=&quot;60&quot; style=&quot;width: 60px&quot; onfocus=&quot;document.LilFormie.Color2.style.width='200px';return false;&quot; onblur=&quot;document.LilFormie.Color2.style.width='60px';return false;&quot;>
        <option value=&quot;red&quot;>red</option>
        <option value=&quot;orange&quot;>orange</option>
        <option value=&quot;yellow&quot;>yellow</option>
        <option value=&quot;green&quot;>green</option>
        <option value=&quot;blue&quot;>blue</option>
        <option value=&quot;indigo&quot;>indigo</option>
        <option value=&quot;violet&quot;>violet</option>
        <option value=&quot;feet&quot;>the same color as my feet</option>
      </select>
      <select name=&quot;Color3&quot; id=&quot;Color3&quot; width=&quot;60&quot; style=&quot;width: 60px&quot; onfocus=&quot;document.LilFormie.Color3.style.width='200px';return false;&quot; onblur=&quot;document.LilFormie.Color3.style.width='60px';return false;&quot;>
        <option value=&quot;red&quot;>red</option>
        <option value=&quot;orange&quot;>orange</option>
        <option value=&quot;yellow&quot;>yellow</option>
        <option value=&quot;green&quot;>green</option>
        <option value=&quot;blue&quot;>blue</option>
        <option value=&quot;indigo&quot;>indigo</option>
        <option value=&quot;violet&quot;>violet</option>
        <option value=&quot;feet&quot;>the same color as my feet</option>
      </select>
      <select name=&quot;Color4&quot; id=&quot;Color4&quot; width=&quot;60&quot; style=&quot;width: 60px&quot; onfocus=&quot;document.LilFormie.Color4.style.width='200px';return false;&quot; onblur=&quot;document.LilFormie.Color4.style.width='60px';return false;&quot;>
        <option value=&quot;red&quot;>red</option>
        <option value=&quot;orange&quot;>orange</option>
        <option value=&quot;yellow&quot;>yellow</option>
        <option value=&quot;green&quot;>green</option>
        <option value=&quot;blue&quot;>blue</option>
        <option value=&quot;indigo&quot;>indigo</option>
        <option value=&quot;violet&quot;>violet</option>
        <option value=&quot;feet&quot;>the same color as my feet</option>
      </select>
      <br /><br />
      <input type=&quot;button&quot; value=&quot;The Chosen Ones&quot; onclick=&quot;var AlertString='The colors you chose are\n\n'+document.LilFormie.Color1.value+'\n'+document.LilFormie.Color2.value+'\n'+document.LilFormie.Color3.value+'\n'+document.LilFormie.Color4.value+'\n\nI hope you enjoy them.';alert(AlertString);return false;&quot;></input>
    </form>
    <p>This works best when you tab around...</p>
  </body>
</html>

Cheers,


[monkey] Edward [monkey]

&quot;Cut a hole in the door. Hang a flap. Criminy, why didn't I think of this earlier?!&quot; -- inventor of the cat door
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top