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
View the demo • Download files
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:
[html]<a href="#">Select</a>[/html]
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.
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’ve shown 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.




Feedback
183 insightful responses to Pretty checkboxes with jQuery
right after event.preventDefault();
thx 4 the informations, good to know.
thx adminim
Estilando checkbox de formularios con jQuery « to live is to die
Howard van Rooijen's Blog » Pretty Check boxes and Radio buttons with jQuery
I’ve tried Sean Foushee’s radio buttons, didn’t work with me =[
Thanks you, good code!
Now that is tidy!
It’s very good. I like this. Thanks for share. And I wrote something to introduce this project for my readers. You can find the post about this in my website. If something is wrong,pls figure it out.thanks.
Pretty checkboxes with jQuery 开源项目 - Ajax代码大全 - Java开源项目 - 表单Form - ajax表单form控件 - Java免费软件 - Pretty-checkboxes-with-jQuery - 开源网
フォームの見栄え・使い勝手を強化するjQueryプラグインいっぱい | DesignWalker | PORTFOLIO -POST批評空間-
This works great and I’ve used it in several projects now…I did change it a little so that you could click on the whole instead of just the link. Plus I made it into one function with an if statement. Just hoping to give back
$(document).ready(function() {
});
Geek is a Lift-Style. » 50 Useful New jQuery Techniques and Tutorials
Thanks for the info. Nice post. Cheers.
very nice thank you man
Thanks you, good code!
thank you.this is the great.i can using my hosting website
20 Must Learn Button Tutorials in jQuery and CSS | blueblots.com
good opera good mozilla
Cool Stuff
thx
Thanx! How can I get the checkbox value after submitting the form? exampel: I select checkbox 1 and 3 and press submit. After the submit all of my my buttons are not selected but I want the checkbox 1 and 3 to still be selected.
good opera good mozilla
[...] Pretty checkboxes with jQuery [...]
my my buttons are not selected but I want the checkbox 1 and 3 to still be selected.
I can using my hosting website. Thank you.this is the great.
50 Useful New jQuery Techniques and Tutorials | Smoshing
Can I add “Check ALL” to this script? and How? Thanks
50 полезных уроков по jQuery (часть 1)
フォームの見栄え・使い勝手を強化するjQueryプラグインいっぱい | DesignWalker
Time to use border-radius as well!
Very cool script.
Now that is tidy..
Oooh, that’s fricking sexy. Me like!
I can using my hosting website. Thank you.this is the great.
Anne ve baba olmak isteyen çiftler için formuluze edilmiş etkili ve %100 bitkisel içeriğe sahip kısırlık ilacıdır
Thank you admin, I love jQuery
Thanks for the information on this topic.
Thank you admin Great Post !
50个有用的jQuery技巧|Narco
45有助于UI界面的有用的jquery教程和技术 | ued二区
50个有用的jQuery技巧 | 前端开发
thx 4 the informations, good to know.
comple awsed returned yokulader nice to the renmpert sizle derhener likeds
terrible myonder again mholder fetret you sharted myonder again mkolder very size
Very nice alternative to standard check boxes!
güzel paylaşım (from turkey)
Thank you for the great example.
Very cool, will be looking to add this to a new feature we are developing for our own site… Nice and tidy CSS! Thanks for sharing!
A really nice blog post thank you, just found you on Google when researching jquery effects
Cool stuff! Used it on one of my projects.
I added this to the function:
$(“.checklist .checkbox-select”).click( function(event) { event.preventDefault(); $(this).parent().addClass(“selected”); $(this).parent().find(“a.checkbox-select”).css({display:’none’}); $(this).parent().find(“a.checkbox-deselect”).css({display:’block’}); $(this).parent().find(“:checkbox”).attr(“checked”,”checked”); } );
$(“.checklist .checkbox-deselect”).click( function(event) { event.preventDefault(); $(this).parent().removeClass(“selected”); $(this).parent().find(“a.checkbox-deselect”).css({display:’none’}); $(this).parent().find(“a.checkbox-select”).css({display:’block’}); $(this).parent().find(“:checkbox”).removeAttr(“checked”); } );
30+ jQuery Forms Plugins to Improve Form Usability - tripwire magazine