Clears Selected Radio Button

by mrrodd

HTML

<input type='radio' class='radio-button' name='re'>
<input type='radio' class='radio-button' name='re'>
<input type='radio' class='radio-button' name='re'>

JavaScript

// allows the clearing of the specified radio button list
var allRadios = document.getElementsByName("re");
var selectedRadio;
var x = 0;
for (x = 0; x < allRadios.length; x++) {
  allRadios[x].onclick = function() {
    if (selectedRadio === this) {
      this.checked = false;
      selectedRadio = null;
    } else {
      selectedRadio = this;
    }
  };
}