views:

40

answers:

1

I am implementing an HTML form. For one of the fields (weather) the user must choose one of a set of options (sunny, cloudy, rainy, etc.)

So basically I'm looking for a nice replacement for <select> or <radio> that provides a series of images (that I will provide) for the user to select from. I'll create dulled/greyed versions of each image that represent unselected items.

I've found loads of jQuery rating controls that provide this, but nothing that quite does what I want (probably because I don't really know what it's called and therefore can't Google it.)

Oh and if the user doesn't have JavaScript enabled, it should degrade nicely to provide standard <select> or <radio> options.

+3  A: 

You could roll your own pretty easily. Start with markup like so:

<fieldset>
    <input type="radio" name="weather" value="sunny" />
    <input type="radio" name="weather" value="cloudy" />
    <input type="radio" name="weather" value="rainy" />
    <input type="radio" name="weather" value="class-3-kill-storm" />
</fieldset>

Without javascript enabled, the users will get the above (you'd probably want to add some label elements so people knew what they were clicking ;). Next, loop through all of them and create the <a> elements you'll need for your icons:

$('input[name=weather]').each(function() {
    var radio = $(this);
    radio.css({display: 'none'});

    var icon = $('<a class="icon ' + this.value + '"></a>');
    icon.click(function(e) {
        // stop default link click behaviour
        e.preventDefault();

        // unmark any previously selected image and mark clicked selected
        icon.siblings('.icon').removeClass('selected');
        icon.addClass('selected');

        // set associated radio button's value
        radio.attr('checked', 'true');
    });
    $(this).parent().append(icon);
});

The reason I'm using an <a> is because IE will properly respect the :hover CSS pseudoclass. Also on that note, I'm using CSS spriting so you'd combine your greyed and full colour images into one 40px tall image with the grey version at the top.

The css for the <a> would look something like:

a.icon {
    float: left;

    /* width and height of your weather icons */
    width: 20px;
    height: 20px;

    background-repeat: no-repeat;
    background-position: 0 0;
}

a.selected,
a:hover.icon {
    background-position: 0 -20px;
}

.sunny{
    background-image: url(sunny.png);
}

.rainy {
    background-image: url(rainy.png);
}

/* remaining weather styles */ 

You can see a version using background colours in action here.

Pat
This looks great. I'll give it a go once I get my view compiling again: http://stackoverflow.com/questions/3550244/asp-net-mvc-strongly-typed-view-compilation-error
Drew Noakes
hah, happens to me all the time - working on the front-end and the back-end starts fighting me.
Pat
Ok finally looking at it now I'm back on track. BTW that http://jsfiddle.net site is fantastic!
Drew Noakes
I have a site running with your approach, though I modified it to use just one sprite image. I'd quite like to bundle it up into a more abstract and reusable component, so will investigate making a jQuery plugin. Thanks for your help with this.
Drew Noakes
Not a problem and good call on converting all icons to sprites. I'd be interested to see the plugin if you get a chance to code it up.
Pat