0

I have 4 radio buttons. When I press one, the others change. But how can I deselect the radio button I selected earlier? I though of this but it doesn't work:

  var id = $(this).attr("id");
  id.setSelected(false);
Igbanam
  • 5,904
  • 5
  • 44
  • 68
VladJ
  • 107
  • 2
  • 10
  • what do you mean?.. when you click a radio button you want the other radio button to be uncheck? – Sam Teng Wong Apr 11 '16 at 08:06
  • It is a question. When I select an answer, the question and the answers change but the button remains selected (so an answer remains selected). I want to reset the radio button so no option is selected for Question number 2. – VladJ Apr 11 '16 at 08:21
  • That's not the way radiobuttons work. You are thinking of checkboxes. You could add a radiobutton with "none of the above" as label, which is better than changing the way a common object is used. – SubliemeSiem Apr 11 '16 at 09:12

2 Answers2

0

Give all radio inputs the same name. See the example below. For this, you don't need JQuery, it's all managed by the browsers.

HTML

<form>
  <input type="radio" name="myRadio">
  <input type="radio" name="myRadio">
  <input type="radio" name="myRadio">
  <input type="radio" name="myRadio">

  <input type="submit">
</form>
Dirk Jan
  • 2,355
  • 3
  • 20
  • 38
0

<body>
    <input type='radio' class='radio-button' name='rb'>
    <input type='radio' class='radio-button' name='rb'>
    <input type='radio' class='radio-button' name='rb'>
</body>

<script type="text/javascript">
    var allRadios = document.getElementsByName('rb');
    var thisRadio;
    var x = 0;
    for(x = 0; x < allRadios.length; x++){

        allRadios[x].onclick = function() {
            if(thisRadio == this){
                this.checked = false;
                thisRadio = null;
            }else{
                thisRadio = this;
            }
        };
    }
</script>

To deselect all radio button you have to add Reset button

Shayan Ghosh
  • 882
  • 6
  • 14