views:

72

answers:

4

Is it possible to have a bunch of <select> dropdowns in html that only display a small (say 10 pixels wide) icon, but when you click it the drop down has a list with the icons beside a descriptive string. (Let's see if ASCII art works on SO):

[X]
| X - Disable |
| v/ - Enable |
| O - Ignore  |
+-------------+
[O]
[v]
[X]

Can that be done in CSS? Or in jQuery?

+3  A: 

This is not possible with a <select> tag in HTML. You'd need to use a custom control provided perhaps with a jQuery plugin. Either way, the entire thing would have to be Javascript and probably wouldn't make use of <select> at all. You could also take a look at ExtJS if you wanted a set of more fully-featured rich controls. It has a Menu class you could use for this.

Marc W
You would need to have an actual select in there too, for accessability and backwards compability. You can then replace it with javascript generated control.
Colin Pickard
+3  A: 

Yes you can do it using jquery. Marghoob Suleman’s Javascript Combo Box plugin for jquery looks like what you want.

image dropdown

It looks like you want a combination of this dropdown with some code similar to BalusC's answer - so that only the image is shown unless the dropdown is active.

Colin Pickard
Very nice. I've never used this one before.
Marc W
No, I don't want to show the text of the selected one, only the icon.
Paul Tomblin
wow that looks like an awesome plugin - thanks!!
Pointy
but you could add javascript to change the css of the selected option… attach to the `OnChange` event, put your option text in a span, change css of selected `span` to `display: none` bam. This will work.
ghoppe
Note: using invisible spans actually doesn't work in *real* option elements.
BalusC
I'd feel a lot happier about this plugin if the links on the guy's page actually worked. He's got a link to version 2.1 on his home page that's broken, and so are all the Javascript links from his "Components and Modules" page.
Paul Tomblin
The links are indeed dead. I could however get a copy by just checking the page source of the demo page and following the URL to the JS: http://www.marghoobsuleman.com/mywork/jcomponents/image-dropdown/jquery-image-dropdown-2.1/msdropdown/js/jquery.dd.js
BalusC
@BalusC - I tried that, and I get an error that Firebug won't actually show me where it is. I'd much rather get a non-minified version, or have some indication that the developer is actually alive and cares about the plugin.
Paul Tomblin
Colin Pickard
+1  A: 

The show/hide part can be done with little help of jQuery. Here's an SSCCE, just copy'n'paste'n'run it.

<!doctype html>
<html lang="en">
    <head>
        <title>SO question 2403303</title>
        <script src="http://code.jquery.com/jquery-latest.min.js"&gt;&lt;/script&gt;
        <script>
            $(document).ready(function() {
                $('select')
                    .bind('mouseover', function() { $(this).addClass('expand').removeClass('clicked'); })
                    .bind('click', function() { $(this).toggleClass('clicked'); })
                    .bind('mouseout', function() { if (!$(this).hasClass('clicked')) { $(this).removeClass('expand'); }})
                    .bind('change blur', function() { $(this).removeClass('expand clicked'); });
            });
        </script>
        <style>
            select { font-family: monospace; width: 35px; }
            select.expand { width: auto; }
        </style>
    </head>
    <body>
        <select><option>X - Disable</option><option>V - Enable</option><option>O - Ignore</option></select><br>
        <select><option>X - Disable</option><option>V - Enable</option><option>O - Ignore</option></select><br>
        <select><option>X - Disable</option><option>V - Enable</option><option>O - Ignore</option></select><br>
    </body>
</html>
BalusC
A: 

Unfortunately, it doesn't work in MSIE, but you can do it with CSS:

select#dropdown option[value="disable"] {
    background-image:url(disable.png);
}
ghoppe