Solution 1 :

They all have different name attribute values – therefore they can only be checked (because they’re not comparing to another sibling radio value). I think you might be looking for type="checkbox" instead:

<input type="checkbox" id="name" name="name"><br>
<input type="checkbox" id="age" name="age"><br>
<input type="checkbox" id="email" name="email"><br>

Solution 2 :

Example: Hold down Ctrl ( on mac) key to uncheck.

var radios = document.getElementsByTagName('input');
for(i=0; i<radios.length; i++ ) {
    radios[i].onclick = function(e) {
        if(e.ctrlKey || e.metaKey) {
            this.checked = false;
        }
    }
}
<input type="radio" name="test" value="1" />
<input type="radio" name="test" value="2" checked="checked" />
<input type="radio" name="test" value="3" />

Solution 3 :

Use <input type="checkbox", not <input type="radio".
Radio button is used where you have to select one of some options (which must have same name).

For example,

<input type="radio" name="gender" id="male" value="male"> <br>
<input type="radio" name="gender" id="female" value="female"> <br>
<input type="radio" name="gender" id="other" value="other">

Know more about radio button and check boxes.

Solution 4 :

 document.querySelectorAll('input[type="radio"]').forEach(x => x.addEventListener('click',function(){
    if(this.value == 0){
          this.checked = true;
          document.querySelectorAll('input[type="radio"]').forEach(x=>x.value=0);
          this.value = 1;
      }
      else {

          this.checked = false;
          document.querySelectorAll('input[type="radio"]').forEach(x=>x.value=0);
          this.value = 0;
      }      
}))
<input type="radio" value="0" id="name" name="test"><br>
<input type="radio" value="0" id="age" name="test"><br>
<input type="radio" value="0" id="email" name="test"><br>

Solution 5 :

This code allows the user to deselect a radio button:

  document.querySelectorAll('input[type="radio"]').forEach(x => x.addEventListener('click',function(){
    if(! this.value0 || this.value0 == 0){
      this.checked = true;
      document.querySelectorAll('input[type="radio"][name="'+this.name+'"]').forEach(x=>x.value0=0);
      this.value0 = 1;
    } else {
      this.checked = false;
      document.querySelectorAll('input[type="radio"][name="'+this.name+'"]').forEach(x=>x.value0=0);
      this.value0 = 0;
    }
  }))

It improves the answer of Sato Takeru.

Problem :

I have some radio buttons in my web page.
when clicking the radio button it checks but when i click again it doesn’t un-check.
i tried to add a JS function onclick

document.querySelectorAll('input[type="radio"]').forEach(x => x.addEventListener('click',function(){
        if(this.checked == true){
            this.checked = false;
        }
        else {
            this.checked = true;
        }
    }))

when I added this method it allowed my to uncheck but didn’t allow me to check any of them!
what can I be missing?

these are my checkboxes:

<input type="radio" id="name" name="name"><br>
<input type="radio" id="age" name="age"><br>
<input type="radio" id="email" name="email"><br>

Comments

Comment posted by Azahar Alam

put a semicolon in the end.

Comment posted by layla l00

JavaScript does not require semicolons necessarily. I tried but it didn’t make any difference.

Comment posted by w3schools.com/jquery/tryit.asp?filename=tryjquery_sel_radio

Refer this,

Comment posted by layla l00

for the link you have shared, the radios have the same name therefore one gets unchecked when you check the other. i.e. you can only select one. in my case the radios are completely unrelated and each of them refers to something different. i am trying to be able to check or un-check any. maybe all. maybe none.

Comment posted by Sato Takeru

if you want select only one you should give the same names. and select several radio buttons you should give different names in my answer. but in the second condition it’s better to use checkbox.

Comment posted by GROVER.

@laylal00 Feel free to mark as having answered your question to help those that might come across the same issue in the future 🙂

Comment posted by layla l00

can i know what does e refer to? is it something i need to define on my browser or just a variable ? because e shows undefined for this solution

Comment posted by Dhrumil shah

e

By