Pretty checkboxes with jQuery
I’ve rewritten some of this so it is valid for XHTML 1.0 Transitional. I also fixed a bug whereby the “pretty” version of the form did not recognize preselected checkboxes.
The core method I show below would also work great for radio buttons. Bagwan Pankaj, Sean Foushee and Philip Beel all have since offered great tutorials on how to extended this concept to radio buttons.
I was working on a project recently where form checkboxes were required, but the standard UI would not do. I needed something a little more engaging. So in this short tut, I’ll show how to turn this…

…into this

Using some CSS and jQuery. You can see this simple demo here. Those new to jQuery should check out these tutorials and resources.
I’m sure you’re psyched. Let’s get started…
1. Form markup
So normally you’d have some structure like this:
All well and good, but we need more. We will use list items to hold the checkboxes, content and controls:
The checkboxes are still there, but we won’t be clicking them. The checking/unchecking will handled by the “Select” and “Cancel” links within each list item.
2. Backgrounds
Each list item has a selected (green) and deselected (grey) state. This is most easily taken care of by using one background image, and shifting the position when needed. Fire up Photoshop and create these images:

The list items will be 105 pixels wide and 150 pixels tall, so checkboxbg.gif should be 210 pixels wide (double the width). When it is selected, the background will shift to the left by 105 pixels to expose the green side. We’ll get to the CSS for this shortly.
Sendit.gif and select.gif will serve as backgrounds for the submit button and select link, respectively.
3. CSS
legend {
font-size: 17px;
}
fieldset {
border: 0;
}
.checklist {
list-style: none;
margin: 0;
padding: 0;
}
.checklist li {
float: left;
margin-right: 10px;
background: url(i/checkboxbg.gif) no-repeat 0 0;
width: 105px;
height: 150px;
position: relative;
font: normal 11px/1.3 "Lucida Grande","Lucida","Arial",Sans-serif;
}
.checklist li.selected {
background-position: -105px 0;
}
.checklist li.selected .checkbox-select {
display: none;
}
.checkbox-select {
display: block;
float: left;
position: absolute;
top: 118px;
left: 10px;
width: 85px;
height: 23px;
background: url(i/select.gif) no-repeat 0 0;
text-indent: -9999px;
}
.checklist li input {
display: none;
}
a.checkbox-deselect {
display: none;
color: white;
font-weight: bold;
text-decoration: none;
position: absolute;
top: 120px;
right: 10px;
}
.checklist li.selected a.checkbox-deselect {
display: block;
}
.checklist li label {
display: block;
text-align: center;
padding: 8px;
}
.sendit {
display: block;
float: left;
top: 118px;
left: 10px;
width: 115px;
height: 34px;
border: 0;
cursor: pointer;
background: url(i/sendit.gif) no-repeat 0 0;
text-indent: -9999px;
margin: 20px 0;
}
As I mentioned above, the effect of adding a class of “selected” to the list item shifts the background image, hides the “Select” link and shows the “Cancel” link. The checkbox itself is also hidden entirely (but it needs to be present in the markup).
4. jQuery
Here’s where we weave this together.
$(document).ready(function() {
/* see if anything is previously checked and reflect that in the view*/
$(".checklist input:checked").parent().addClass("selected");
/* handle the user selections */
$(".checklist .checkbox-select").click(
function(event) {
event.preventDefault();
$(this).parent().addClass("selected");
$(this).parent().find(":checkbox").attr("checked","checked");
}
);
$(".checklist .checkbox-deselect").click(
function(event) {
event.preventDefault();
$(this).parent().removeClass("selected");
$(this).parent().find(":checkbox").removeAttr("checked");
}
);
});
});
Line 4
Looks to see if there are any pre-checked checkboxes, and assigns the appropriate class if there are any.
Line 7
This selector grabs all those green “Select” buttons and assigns a script to the click event.
Lines 9, 10, 11
We want to prevent the link from invoking its default behavior of reloading the page, and continue with our instructions. The $(this) object refers to the “Select” button itself:
Select
So $(this).parent() refers its parent in the DOM, which is the <li> element. This is the element we want to add a class of “selected” to. Finally, line 11 makes sure that the checkbox is actually selected by setting its “checked” attribute.
Line 16
This selector catches all the “Cancel” links (which become visible when the list item’s class is set to “selected”).
Lines 18, 19, 20
This essentially undoes everything we did in lines 9, 10 and 11 above.
So what can you do with this?
Anything you want. You can keep things simple and familiar by creating custom designed checkboxes, or take a more innovative approach and employ a more creative multiple selection UI. You could even create a toggle switch like those found in the iPhone settings. This tutorial is more of a starting point. As I mentioned above, there are some other tutorials you can check out based on this concept applied to radio buttons.
[...] Url: http://aaronweyenberg.com/90/pretty-checkboxes-with-jquery [...]
[...] · Tutorial: Ver Enlace [...]
[...] · Tutorial: Ver Contenido‘ [...]
Great stuff !
These checkboxes are really pretty
[...] · Tutorial: Ver Contenido [...]
Good…. Que Bueno—
how can i apply this for radio buttons? but if checked one of radio button others are must be unchecked…
[...] Pretty checkboxes with jQuery [...]
@emir_akin, hey amir check this blog
http://railsjaipur.wordpress.com/2009/09/12/pretty-radio-buttons/
for extension of this blog.
still if u have any confusion then please contact me on bagwanpankaj@gmail.com
Great tutorial Aaron, looks great, I’m currently working on inserting this into a project. Have you managed to sort this out with client side validation?
Emir, read through the links from Sean. It’s a similar tutorial for radio button.
[...] Pretty checkboxes Une méthode pour remplacer de manière innovante les « bêtes » cases à cocher. Propre et clair ! [...]
hi,
looks good, but it if you have javascript disabled it wont work…. i think for a webform it is important to work, or your guests cant communicate with you, isnt it?
[...] Pretty checkboxes with jQuery | Aaron Weyenberg – De très chouettes cases à cocher avec jQuery [...]
[...] recently read a fantastic tutorial by Aaron weyenberg which shows how to make checkboxes more visually appealing. This got me thinking, if checkboxes, [...]
Great example of checkbox replacement.
Works perfectly for prototyping, but I would not publish this version on any commercial site. Problems were the duplicate XHTML markup that appears without CSS and Javascript dependency.
Sean Foushee did great job as he solved these issues. Check his comment above.
Thanks to Aaron for inspiration and to Sean for valid code. Good work guys.
Great post. I wery much learn from this post.
THX so much.
Very Nice idea!非常感谢你的创意,非常棒
[...] Pretty checkboxes with jQuery [...]
[...] Tutorial Tutorial Page [...]
[...] Pretty checkboxes with jQuery [...]
This is awesome….
Thankz!!
one validation error: the tag is not meant to wrap around the entire group of checkboxes. Instead, it is meant to wrap around each checkbox’s individual label, like so:
================================================
Choice one
Choice two
Choice three
Choice four
Send it!
====================================================
This allows the browser to select the checkbox even when a user selects the associated label. Even though you are using jQuery to override the behavior, you should still use this correctly for properly degrading code on machines w/out JS.
[...] i required some pretty checkboxes as well as some radio buttons. I googled on the web and found aaron’s blog. but that script just supports the checkboxes. It was really fascinating but i needed radio button [...]
I need to check all Checkbox at once… Thanks
you’r very very good and very very nice.
please send to me. think you very mach!!!!!!
nice collect it to
ajax.wespai.com
[...] Pretty Checkboxes with jQuery [...]
Great tutorial
bookmarked
Great tutorial just del.icio.us it
Thanks
This is great. The only problem I’m having is that when I open up the page in ColorBox (http://colorpowered.com/colorbox/), it’s broken for me in FF and Chrome. (It works in IE7.) Can you think of any reason offhand why this would happen?
Amazing! Thanks for share!
woW… cool man, thanks… it’s usefull…
Hi, my checkbox default are checked but it seems that it doesn’t show that it is checked onload of the page. How can I fix it? Thanks so much for this great work.
@McKenzie
I have addressed this issue and the code will now support that.
@Jen S.
I have not used ColorBox with this, or any other plugin in combination actually. I’m amazed that something works in IE7 but not in FF or Chrome though! When I have some extra time I’ll look into it.
@Hubert
Indeed, I was kind of embarrassed about that so I did fix it so it is completely valid.
[...] Pretty Checkboxes with jQuery [...]
[...] Pretty Checkboxes with jQuery [...]
[...] Pretty Checkboxes with jQuery [...]
[...] Tutorial | Demo [...]
Really nice bit of design and code there, you could improve it visually even more by putting “outline:none;” into your “a.checkbox-deselect {” section, as when the images are clicked a nasty outline ruins the asthetics.
@Dan
Thank you – and yes you’re right, good catch. That was a careless miss indeed. I keep forgetting that not everyone uses my browser
[...] Pretty Checkboxes with jQuery [...]
I currently have several checkboxes that are controlled by Javascript.
There are 4 checkbox at the top. 1 toggles all checkboxes on and off, the other 3 toggle groups of checkboxes based on their id.
I’m considering using JQuery/Pretty Checkboxes to jazz the checkboxes up a bit.
Is it possible to do this given that some boxes have a toggle function attached to them?
Or would I need to recreate from scratch and create new JQuery functions to handle the toggling?
Thanks
@iltdev
What I have written is a general guide that works with the standard checkbox behavior. If I understand your requirements correctly, you will need to write new jQuery functions to handle the unique behavior of those checkboxes. You’d need to have a separate jQuery statement to handle a checkbox which changes the state of other multiple checkboxes.
Nice tutorials, thanks.