I would like to create an HTML form for user feedback. If the overall feedback is good, the user should click on a laughing smiley, if the overall feedback is bad, the user
You cannot style things like radio buttons, checkboxes, scrollsbars (etc.) at all. These are native to the OS and the browser and not something you can manipulate.
You can simulate this, however by hiding the radio buttons and only showing an image instead as in.
<input type="radio" style="display: none;" id="sad" /><label for="sad"><img class="sad_image" /></label>
With pure html (no JS), you can't really substitute a radio-button for an image (at least, I don't think you can). You could, though use the following to make the same connection to the user:
<form action="" method="post">
<fieldset>
<input type="radio" name="feeling" id="feelingSad" value="sad" /><label for="feelingSad"><img src="path/to/sad.png" /></label>
<label for="feelingHappy"><input type="radio" name="feeling" id="feelingHappy" value="happy" /><img src="path/to/happy.png" /></label>
</fieldset>
</form>
another alternative is to use a form replacement script/library. They usually hide the original element and replace them with a div or span, which you can style in whatever way you like.
Examples are:
http://customformelements.net (based on mootools) http://www.htmldrive.net/items/show/481/jQuery-UI-Radiobutton-und-Checkbox-Replacement.html
Let's keep them simple, shall we. First off, using pure HTML + CSS:
<div id="emotion">
<input type="radio" name="emotion" id="sad" />
<label for="sad"><img src="sad_image.png" alt="I'm sad" /></label>
<input type="radio" name="emotion" id="happy" />
<label for="happy"><img src="happy_image.png" alt="I'm happy" /></label>
</div>
This will degrade nicely if there's no JavaScript. Use id
and for
attributes to link up the label and radiobutton so that when the image is selected, the corresponding radiobutton will be filled. This is important because we'll need to hide the actual radiobutton using JavaScript. Now for some jQuery goodness. First off, creating the CSS we'll need:
.input_hidden {
position: absolute;
left: -9999px;
}
.selected {
background-color: #ccc;
}
#emotion label {
display: inline-block;
cursor: pointer;
}
#emotion label img {
padding: 3px;
}
Now for the JavaScript:
$('#emotion input:radio').addClass('input_hidden');
$('#emotion label').click(function(){
$(this).addClass('selected').siblings().removeClass('selected');
});
The reason why we're not using display: none
here is for accessibility reasons. See: http://www.jsfiddle.net/yijiang/Zgh24/1 for a live demo, with something more fancy.