Last weekend I spent some time cleaning up a script that I had written that would hide Checkbox and Radio Button Inputs and allow you to add CSS styling to the label element. I decided that I would post the script on digg.com to help raise some awareness and drive traffic to the script. In the first 24 hours I received 42 diggs and I was in the top 3 most popular stories in their upcoming stories section. I was quite happy about this moderate success, however was thrilled to see the response the next day.
Yesterday, The day after I added the script to digg, CRIR ended up on delicious, stumbleupon, reddit, cssdrive, and a handful of blogs. I honestly couldn't believe how well received this script was and how much traffic my site was getting because of this one small script. The script was on the popular page at delicious all day, it was in the top 50 on reddit most of the day, it received over 1500 thumbs up on stumbleupon, and people were leaving some great comments about the script all over the place. By the end of the day yesterday 4600 people from all over the globe had visited my site to check out the script, and as I write this at 1:00 pm there has been over 1500 people visiting today. This is a huge increase from the 30 - 50 visitors I get on an average day.
I also received an email from Jesse Gavin this morning informing me that he made some improvements to the script. He converted to script JSON (JavaScript Object Notation) and included an AddEvent function which eliminates the need to initialize the script using body onload. I've uploaded these updates to the script so be sure to go grab the latest version of the script.
With the amount of attention the scipt was recieving it was normal to get some critism. Some skeptical people said that because the inputs no longer look like traditional Checkboxes or Radio Buttons it would be hard for people to understand that elements are clickable inputs. While this may be true with the sample that I provided, it's up to the designer/developer that's using the script to make the inputs appear more like the traditional inputs. The script allows the designer/developer to get creative and design Checkboxes and Radio Buttons that match the rest their site's design. Other critisim was the fact that once the inputs are replaced you can no longer use your keyboard to tab to them and use the spacebar to check them. This is also very true and it may be something to fix later when I get ambitious.
I'm really looking forward to seeing what people do with the script. Be sure to drop me a line and let me know how you've used the script.
Go check out the script here: CRIR Checkbox and Radio Input Replacement
Good stuff. I wrote the same thing about a year ago (before I learnt the ways of JSON unfortunately).
My script adds a bit more accessability stuff – like you can tab onto the checkbox elements and it outlines it. Perhaps you’d like to have a look at take some of those features back into your code.
http://www.flog.co.nz/lab/ARC/ARC.htm
did this a while back – same thing really; hide input and put a class on the label (but in only 33 lines):
@var transForms = {
init: function () {
var ls = document.getElementsByTagName(“label”);
for (var i=0, l; l=ls[i]; i++) transForms.labels[l.htmlFor] = l;
var e = document.getElementsByTagName(“input”);
for (var i=0, o; o=e[i]; i++) {
switch (o.type) {
case “checkbox”:
case “radio”:
o.style.display = “none”;
o.onchange = transForms.checkState;
transForms.setState(o);
break;
default:
break;
}
}
},
checkState: function () {
var r = [];
if (this.type==”radio”) {
var e = document.getElementsByTagName(“input”);
for (var i=0, o; o=e[i]; i++) if (o.name==this.name) r.push(o);
}
else r.push(this);
for (var i=0, o; o=r[i]; i++) transForms.setState(o);
},
setState: function (o) {
transForms.labels[o.id].className = o.type + ((o.checked) ? “Checked” : “”);
},
labels: []
}@
initiate calling transForms.init any way you like (body onload, window.onload, addevent…etc)
@Adam:
Thanks for the heads up I really appreciate it. I will take a look at your script and see if I can get the tabbing/space bar stuff working soon. That’s the major shortcoming of my CRIR right now.
@rasmus:
Does your script work in all browsers? how do the labels control the inputs when the inputs get hidden? This was one area that bothered me while writing this script. In Firefox even though an input is hidden the label will still trigger the input. However the same is not true for Internet Explorer and other browsers. So this fact alone adds some complexity to the script. I really wish all browsers worked the same way! Thanks for your input though, I may borrow some ideas from your script in the future.
Sure enough – I cut a corner and Internet Explorer bites my backside. ;)
My original tests also had inputs styled with zero opacity instead of having display: none.
But it’s fixed fairly easily; just add a style class “transFormsHidden” to the inputs looking like so:
.transFormsHidden {
position: absolute; left: -3000px; opacity: 0.0; _filter: alpha(opacity=0);}
...and the fixed script:
var transForms = {
init: function () { var ls = document.getElementsByTagName(“label”); for (var i=0, l; l=ls[i]; i++) transForms.labels[l.htmlFor] = l; var e = document.getElementsByTagName(“input”); for (var i=0, o; o=e[i]; i++) { switch (o.type) { case “checkbox”: case “radio”: o.className = “transFormsHidden”; o.onchange = transForms.checkState; o.onclick = transForms.checkState;transForms.setState(o); break; default: break; } } }, checkState: function () {
var r = []; if (this.type==”radio”) { var e = document.getElementsByTagName(“input”); for (var i=0, o; o=e[i]; i++) if (o.name==this.name) r.push(o); } else r.push(this);
for (var i=0, o; o=r[i]; i++) transForms.setState(o); }, setState: function (o) { transForms.labels[o.id].className = o.type + ((o.checked) ? “Checked” : “”); }, labels: [] }
Very nice and elegant stuff Chris – I was resigned about this topic but now the sky become more shiny ;)
Very nice, but what, if i have 4 pairs of icons, for display like radio buttons, in your css you have label.radio_checked, and other label.radio_unchecked , with respective image, but if i have 8 images, of 4 pairs of images. like some example:
Quiz of videogames:1.- Fast: ( +_ +) ( ’_’ ) ( O_o) ( U_U)
2.- Funny ( +_ +) ( ’_’ ) ( O_o) ( U_U)
etc, and when the user select one, this convert in image checked. I need to do that, but, need help.