Archive for April 10th, 2012

Making radio buttons deselectable, like checkboxes

Tuesday, April 10th, 2012

Here’s a quick hack to give a set of radio buttons the ability to each turn off like a checkbox would if clicked when already selected. It requires JQuery and you can see it in action on JSFiddle.

$('[id*=radio_]').click(function() {
previousValue = $(this).attr('currentValue');

if (previousValue == "ON")
$(this).attr('currentValue', 'OFF');
} else {
$(this).attr('currentValue', 'ON');

var currentId = $(this).attr('id');

if ($(this).attr('id') != currentId)
$(this).attr('currentValue', 'OFF');