One of the problems I discovered with this is the switch groups. The presented solutions work fine for one switch, but in groups I ran into a problem when deselecting one of them and then trying to select another failed (until the second click).
I just found a solution here that works fine:
var allRadios = $('input[type=radio]') var radioChecked; var setCurrent = function(e) { var obj = e.target; radioChecked = $(obj).attr('checked'); } var setCheck = function(e) { if (e.type == 'keypress' && e.charCode != 32) { return false; } var obj = e.target; if (radioChecked) { $(obj).attr('checked', false); } else { $(obj).attr('checked', true); } } $.each(allRadios, function(i, val){ var label = $('label[for=' + $(this).attr("id") + ']'); $(this).bind('mousedown keydown', function(e){ setCurrent(e); }); label.bind('mousedown keydown', function(e){ e.target = $('#' + $(this).attr("for")); setCurrent(e); }); $(this).bind('click', function(e){ setCheck(e); }); });
source share