iPhone Style Radio and Checkbox Switches using jQuery and CSS
For my latest project, I created a great interface for an administration panel, complete with great looking forms and buttons. The one thing that stuck out like a sore thumb, however, were the ugly radio buttons and checkboxes I had to use repeatedly throughout the panel, so I decided to fix them. I found a few plugins that achieved what I wanted, however they were all pretty hefty in file size. My version doesn’t look as cool but makes up for it in file size, weighing less than 3kb and using only one image.
The Effect in Action:
This is in an iframe, however you can also view the page itself. In my example, I left the actual input fields visible, in a real world use you should hide them using CSS. Feel free to reuse the source code, or follow the steps below:
Prerequisites
You need just two things for this to work correctly: JQuery 1.3.2+ and the switch.gif image file used for the backgrounds (both included in the zip file). After you have those two files, follow the steps below:
Step 1 The HTML
1 2 3 4 5 6 7 8 9 10 11 | <p class="field switch"> <input type="radio" id="radio1" name="field" checked /> <input type="radio" id="radio2" name="field" /> <label for="radio1" class="cb-enable selected"><span>Enable</span></label> <label for="radio2" class="cb-disable"><span>Disable</span></label> </p> <p class="field switch"> <label class="cb-enable"><span>On</span></label> <label class="cb-disable selected"><span>Off</span></label> <input type="checkbox" id="checkbox" class="checkbox" name="field2" /> </p> |
Step 2 The Javascript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | $(document).ready( function(){ $(".cb-enable").click(function(){ var parent = $(this).parents('.switch'); $('.cb-disable',parent).removeClass('selected'); $(this).addClass('selected'); $('.checkbox',parent).attr('checked', true); }); $(".cb-disable").click(function(){ var parent = $(this).parents('.switch'); $('.cb-enable',parent).removeClass('selected'); $(this).addClass('selected'); $('.checkbox',parent).attr('checked', false); }); }); |
Step 3 The CSS
1 2 3 4 5 6 7 8 9 10 | .cb-enable, .cb-disable, .cb-enable span, .cb-disable span { background: url(switch.gif) repeat-x; display: block; float: left; } .cb-enable span, .cb-disable span { line-height: 30px; display: block; background-repeat: no-repeat; font-weight: bold; } .cb-enable span { background-position: left -90px; padding: 0 10px; } .cb-disable span { background-position: right -180px;padding: 0 10px; } .cb-disable.selected { background-position: 0 -30px; } .cb-disable.selected span { background-position: right -210px; color: #fff; } .cb-enable.selected { background-position: 0 -60px; } .cb-enable.selected span { background-position: left -150px; color: #fff; } .switch label { cursor: pointer; } .switch input { display: none; } |
Alternatively, you could use a single hidden input field and change the value of it using JQuery. I used this method mainly to illustrate the effect works, as you can actually see the form values changing (to make sure it works).
I know it doesn’t have the fancy animation effects of the alternatives but give it a shot and let me know what you guys think!
Edit: If you look back a few posts, I mentioned that the new JQuery UI includes this functionality. While I considered it for this project, I didn’t need any of the other features offered by JQuery UI, so I opted to create my own simpler version (and save 30-40kb in file size).
Enjoyed the post?
Then you should follow me on Twitter or subscribe to the RSS feed.


Pingback: iPad Links: Friday, March 26, 2010 « Mike Cane's iPad Test
Pingback: jQueryとCSSで作るiPhone風ラジオボタン・チェックボックス | Webクリエイターボックス
Pingback: Heti link #1 « WebDesigner a köbön
Pingback: Inspiration
Pingback: ” 40个有用的jQuery插件 – HTML5,CSS3,WEB前端资讯站
Pingback: 40 Useful jQuery Techniques and Plugins « R@j@. R.K
Pingback: 40 herramientas jQuery | Diseño web, accesibilidad, usabilidad, posicionamiento y optimización web - AlmacenPlantillasWeb Blog
Pingback: 20 Awesome jQuery Enhanced CSS Button Techniques - Speckyboy Design Magazine
Pingback: 40 Useful jQuery Techniques and Plugins « SaiSatheesh
Pingback: jQuery Enhanced CSS Button Techniques
Pingback: Bookmarks Juni: 10 Handige Design resources | Koen van der Zanden
Pingback: ” 20+jQuery增强型CSS按钮– HTML5,CSS3,WEB前端设计开发资讯站
Pingback: フォームの見栄え・使い勝手を強化するjQueryプラグインいっぱい | DesignWalker | PORTFOLIO -POST批評空間-
Pingback: 40 Useful jQuery Techniques and Plugins | Best Web Magazine
Pingback: 40 Useful jQuery Techniques and Plugins | Geek is a Lift-Style.
Pingback: 25 Cool and Helpful jQuery Plugins/Tutorials For Your Next Project | DesignBeep
Pingback: 20 Must Learn Button Tutorials in jQuery and CSS | blueblots.com
Pingback: 20+jQuery增强型CSS按钮 | 俊顶视觉设计网 - 视觉创意,前端开发,web标准设计,高端网站,网页设计教程,网站策划,网站推广,网络营销。
Pingback: 25 Amazing jQuery Plugins | web3mantra
Pingback: 8 jQuery Plugins/Tutorials For Styling Custom Radio Button and Checkbox
Pingback: 智慧本 » 20必须学会的jQuery和CSS按钮教程
Pingback: フォームの見栄え・使い勝手を強化するjQueryプラグインいっぱい | DesignWalker
Pingback: 12 More Creative & Useful jQuery Plugins « The Creative Project
Pingback: 5 ciekawych pluginów jQuery | wordpress frontend & css tips - 960Pixels netpress
Pingback: Coding for Everyone – Emre
Pingback: 40种有用的jQuery技巧及插件|Narco
Pingback: 40 Really Great jQuery Techniques And Plugins | SCRIPTSBYTES
Pingback: 15 интересных jQuery плагинов | javadav.com
Pingback: Plugins Jquery - Grids, TreeView, Calendario, Gmaps, Gráfico | Diário da Fonte
Pingback: Web developper TN : journal du développement web- développeur web freelance » Blog Archive » Plugins jQuery pour Styling les Checkbox & les boutons radio
Pingback: jQuery Plugins for Styling Checkbox & Radio Buttons | Pixelated Indian
Pingback: jQuery Plugins for Styling Checkbox & Radio Buttons | ProDesigning Blog
Pingback: 美化Checkbox&Radio的jQuery插件 | 百锐网
Pingback: 漂亮 Checkbox & Radio 的插件 » 一叶`知秋
Pingback: 推荐!美化Checkbox的11款jQuery插件 | 绝妙网
Pingback: 美化Checkbox?有木有 « csdn.me
Pingback: 告别呆板—美化Checkbox的11款jQuery插件推荐 | 刀侠剑客
Pingback: ศึกษาการทำ Radio and Checkbox แบบ iPhone ด้วย JQuery และ CSS | Nirak 's BLOG
Pingback: 告别呆板---美化Checkbox的11款jQuery插件推荐 | 顾大侠
Pingback: 美化Checkbox的11款jQuery插件推荐 | 刘召考的博客 | 童话天使的博客 | Kevin's Blog
Pingback: 美化Checkbox的11款jQuery插件 | PM
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2586th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2614th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2645th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2675th Edition Akira Designs
Pingback: 【分享】11款国外超级棒的iPhone风格jquery表单插件[jeking.name] | JeKing‘s Blog - 深圳Flash制作|深圳Flash建站|深圳Flash开发|深圳 Flash UI制作
Pingback: 推荐11款jQuery表单元素美化插件 - bruce's blog - WEB开发,SEO,互联网技术应用
Pingback: 11个jQuery表单元素美化插件 | Mybabyweb
Pingback: 11 款 jQuery 的复选框和单选框美化插件 | 探索
Pingback: 推荐11款jQuery的复选框和单选框美化插件 | 百锐网
Pingback: 推荐11款jQuery的复选框和单选框美化插件 » 托尼前端开发
Pingback: 亿脑儿: 创意的那些事[innoor.com]
Pingback: 40种有用的jQuery技巧及插件 | ajax-js – ajax实例汇总
Pingback: 关于复选框和单选按钮的jQuery插件 - WEB开发笔记
Pingback: 11个jQuery表单元素美化插件 - Web前端训练营
Pingback: Simple Cross Browser Jquery Plugins | Blog | Lform Design | We Take Your Vision Beyond Your Imagination
Pingback: 10 jQuery Enhanced CSS Buttons | jQuery4u
Pingback: 17 Useful jQuery Plugins to Improve the Look and Feel of Your Website - DesignModo
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 1702th Edition Akira Designs
Pingback: 推荐11款jQuery的复选框和单选框美化插件 - 前端开发
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 1805th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 1836th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 1866th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 1893th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 1924th Edition Akira Designs
Pingback: .::ich-wars-nicht.ch::. » jQuery On-Off Toggle/Switch
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 1955th Edition Akira Designs
Pingback: Tips and Tricks To Imrove jQuery | DesDevWeb
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 1983th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2014th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2045th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2072th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2102th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2133th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2192th Edition Akira Designs
Pingback: » jQuery Plugins for Styling Checkbox & Radio Buttons – 2223th Edition Akira Designs
Pingback: 25 Cool and Helpful jQuery Plugins/Tutorials For Your Next Project | free