Décocher un bouton radio sélectionné en cliquant dessus. Source: How to make a radio button unchecked by clicking it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function deselectableRadios(rootElement) { if(!rootElement) rootElement = document; if(!window.radioChecked) window.radioChecked = {}; window.radioClick = function(e) { const obj = e.target, name = obj.name || "unnamed"; if(e.keyCode) return obj.checked = e.keyCode!=32; obj.checked = window.radioChecked[name] != obj; window.radioChecked[name] = obj.checked ? obj : null; } rootElement.querySelectorAll("input[type='radio']").forEach( radio => { radio.setAttribute("onclick", "radioClick(event)"); radio.setAttribute("onkeyup", "radioClick(event)"); }); } deselectableRadios( $_IMPRINT_LIST ); |