Click to See Complete Forum and Search --> : Select Box with a horizontal scroll
briwest1
02-03-2003, 09:41 AM
Is there any way to create a select list box with a horizontal scroll bar. I know how to set a style to make it a fixed width, but if there is overflow I do not see it. Here is what I have so far:
<html>
<head>
<title>Select Box</title>
<style type="text/css">
.selectBox { font-size: xx-small; ; width: 150px}
</style>
</head>
<body>
<select class="selectBox" name="select" size="7">
<option value="1">I with I was in the land of cotton where old times there are not forgotten</option>
<option value="2">This is value 2</option>
<option value="3">This is value 3</option>
<option value="4">This is value 4</option>
<option value="5">This is value 5</option>
</select>
</body>
</html>
Thanks in advance for any help I recieve.
Zach Elfers
02-03-2003, 09:48 AM
You can't make a horizontal scrollbar in a select box. Why would you want to anyway?
Add 'overflow: scroll' or 'overflow: auto' to your rule depending on what you want to accomplish.
http://www.w3.org/TR/REC-CSS2/visufx.html#propdef-overflow
Don't think it works correctly in other browsers than Moz though.
briwest1
02-03-2003, 12:31 PM
Meow...thanks for the attempt but I think you are right. They might work in Moz and I need it to work in IE.
Zach...The reason I need it is because I have limited space on a page and the info I need to display can be up to 100 characters long. Cutting it off at 30 char and putting "..." at the end is not an option either, so I was hoping to give our users a scrollbar if they need to see the whole label.
Vladdy
02-03-2003, 01:19 PM
If it's ok for you to rely on javascript, you can emulate list box using a div element, and some code for item selection...
Other approach is to reconsider your page layout: if that information is as important as you say it is, it should not be squeezed into a small box ...
Stefan
02-04-2003, 01:48 AM
Originally posted by briwest1
I have limited space on a page and the info I need to display can be up to 100 characters long.
You could try putting the entire <form> inside a div with {overflow:auto;}
briwest1
02-04-2003, 08:32 AM
Thank you all for your help...but it definitely needs to be a select box and mocking up a div element won't be good enough. It looks like this is a case where it is just not possible and I may have to reformat my page.
Again...thanks for your suggestions.
Stefan
02-05-2003, 05:15 AM
Originally posted by briwest1
It looks like this is a case where it is just not possible and I may have to reformat my page.
Did you try specifying overflow:auto on the <form> ? :confused:
briwest1
02-05-2003, 08:57 AM
Yes Stefan, I tried it. It did not work for a select box and having an entire form scroll is not an acceptable alternative. If you can make it work and provide a coded example of it working then you're the man.
Vladdy
02-05-2003, 09:32 AM
Originally posted by briwest1
mocking up a div element won't be good enough.
With right coding and styles div element can be made MUCH better than a standard select list... Ever considered a posibility of a tooltip to popup when user mouseovers the long item???
briwest1
02-05-2003, 09:37 AM
Intriguing Vladdy...is there any example that you can give me on how I could go about doing this? Even if you show me a site that has it or a site that tells how to do this would be helpful.
Vladdy
02-05-2003, 11:55 AM
Just something I whipped up during lunch hour. For now IE only (tested in IE6.0), but with added browser recognition and substituting attachEvent with addEventListener it can be made DOM compliant and would work with Mozilla and other Gecko based browsers.
The beauty of the shown approach is that it replaces the existing select controls with size attribute set to 2+ (those that are represented as a list box) with the div element. Just need to put upgradeSelect() function to the body onload. If visitor has JS disabled or uses old or non-compliant browser the default select box will be displayed.
To make the form operational some additional code is required. A hidden input element needs to be created with the same name as the select box. It's value would be set when processing option onclick event with the value of that option. This would make the change transparent to the script processing the form.
As you can see from the code all the styles for the box, option and selected option are completely customizable. The default tooltip pops up after you hover over an option for a few seconds, but custom tooltips can be used to show immideately and only for those items that are too long for the box.
You can also change the overflow property to have both scrollbars as you originally requested.
Functionality to recognize the OPTGROUP tag can also be added to make the emulation complete.
If the code below is over your head to work with, I can finish the coding for you (at a discount rate ;) )
<html>
<head>
<title>ListBox Upgrade</title>
<script>
function upgradeSelect()
{ scs=document.getElementsByTagName('select');
for(i=0; i<scs.length; i++)
if(parseInt(scs[i].size)>1)
new KLSelect(scs[i]);
}
function KLSelect(sc)
{ kls=document.createElement('div');
kls.className = sc.className;
for(i=0; i<sc.options.length; i++)
{ with(kls.appendChild(document.createElement('p')))
{ appendChild(document.createTextNode(sc.options[i].firstChild.nodeValue));
if(sc.options[i].selected) className = 'KLOptionSelected';
else className = 'KLOption';
optionValue = sc.options[i].value;
title = firstChild.nodeValue;
attachEvent('onclick',KLOptionClicked);
}
}
sc.parentNode.replaceChild(kls,sc);
}
function KLOptionClicked(e)
{ with(e.srcElement.parentNode)
for(i=0; i<childNodes.length; i++)
childNodes[i].className = 'KLOption';
e.srcElement.className = 'KLOptionSelected';
}
</script>
<style>
.KLSelect
{ width: 125px;
height: 50px;
background: #eeeeee;
border: groove #808080 3px;
font-family: Tahoma, sans-serif;
font-size: 11px;
overflow: auto;
overflow-x: hidden;
}
.KLOption, .KLOptionSelected
{ margin: 0px;
padding: 0px 5px 0px 3px;
width: 100%;
white-space: nowrap;
cursor: pointer;
cursor: hand;
}
.KLOptionSelected
{ background: #303030;
color: #ffffff;
}
</style>
</head>
<body>
<p>Example of emulating select control with division element</p>
<select size="4" name="test" class="KLSelect">
<option value="1" selected>option 1</option>
<option value="2">option 2</option>
<option value="3">option 3</option>
<option value="4">option 4</option>
<option value="5">option 5 which is pretty darn long </option>
<option value="6">option 6</option>
</select>
<p>
<button onclick="upgradeSelect()"> Replace Select Control</button>
</p>
</body>
</html>
Stefan
02-06-2003, 09:47 AM
Originally posted by briwest1
Yes Stefan, I tried it. It did not work for a select box and having an entire form scroll is not an acceptable alternative. If you can make it work and provide a coded example of it working then you're the man.
This works fine for me in IE 6, Moz and Opera 7
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<meta http-equiv="Content-Style-Type" content="text/css">
<title></title>
<style type="text/css">
form {border:1px solid blue;}
div {border:1px solid red; width:150px; overflow:auto;}
.selectBox { font-size:xx-small;overflow:auto;}
</style>
</head>
<body>
<form>
<div>
<select class="selectBox" name="select" size="7">
<option value="1">I with I was in the land of cotton where old times there are not forgotten</option>
<option value="2">This is value 2</option>
<option value="3">This is value 3</option>
<option value="4">This is value 4</option>
<option value="5">This is value 5</option>
</select>
</div>
</form>
</body>
</html>
briwest1
02-07-2003, 09:18 AM
Stefan I do appreciate your help, but what you just gave me was smoke and mirrors. If you add 10 more options on the end of it and scroll all the way to the right you'll see why.
Vladdy, I tried your method but I could not get it to work. I read your instructions to a tee but no horizontal scroll bar appears on the select box. Not sure what I am missing or if you tried this with something else added that I'm not seeing. If you can put it up on geocities or some free web host where I can see it working I'd be happy to try it further.
Thanks for all your help guys but I really don't think this is possible. We have resolved to go another route to fix this. If you want to try and make this work go for it, but I'm surrendering for time constraints on deadlines.
Thanks again for your time.
Stefan
02-07-2003, 09:45 AM
Originally posted by briwest1
Stefan I do appreciate your help, but what you just gave me was smoke and mirrors. If you add 10 more options on the end of it and scroll all the way to the right you'll see why.
:confused:
You will have to explain what your problem is, or I cannot offer a way to solve it.
At least I don't have any problem accessing any of the info with 15 options.
Vladdy
02-07-2003, 11:25 AM
copy the code I posted to a notepad.
delete the following line in the .KLSelect declaration
overflow-x: hidden;
save it as something.html
check it in IE (tested in 6.0, should work with 5+).
Oh and do not forget to click "Replace Select Control" button for changes to take effect.
Vladdy
02-07-2003, 07:07 PM
Here is an improved demo (still IE only):
http://www.vladdy.net/tests/lbie.html
nehagoel28
06-21-2007, 04:29 AM
I also have the same problem that my content in drop down is not fixed and it increases the width of page.So i tried your code but i m not able to get it .
Please help
neha
Greywacke
11-02-2011, 12:44 PM
you might want to try giving the individual option tags within the select tag, a title attribute, containing the full text.
i am not sure in which browsers all this is supported though, but i have tested it in mozilla firefox - i myself have had problems with long texts within listboxes - and being unable to scroll them in any browser.
other than that - developing your own "listbox" control with div tags is the only option.