title: How to uncheck a radio button by clicking it again in vanilla js
body: <p>I have a list of radio buttons and the obvious action is that the inputs checked value becomes true when it's clicked.</p>
  <p>I have seen this link (<a href="https://stackoverflow.com/questions/19895073/radio-button-uncheck-on-second-click">Radio button uncheck on second click</a>) and although jquery is basically javascript, the solution was a bit confusing and I was hoping for a more modern javascript solution.</p>
  <p>I have seen suggestions about using checkboxes but I assume it won't work in my case as a checkbox would allow multiple inputs and that wouldn't work on ratings.</p>
  <p>I would like to uncheck the radio button when I click again on it. The initial issue was that the first input could not be unchecked and I found this solution (<a href="https://stackoverflow.com/questions/58039721/issue-with-star-rating-css">Issue with star rating css</a>) but then I realised that it's not just for the first one that I need this option.. it's for all inputs.. and the above link, it seems (I am not 100% sure), unchecks the first input button wherever I may click.. and that's not what I am looking for. The input state should be maintained if the user clicks anywhere on the document. But if the user feels that he needs to uncheck his current action, then he would try to click on the selected star/input again and I want the radio button to be unchecked at this point.</p>
  <p>I tried to create the function in Javascript with the logic that if the checked status of the clicked button is true then change it to false but that's not logical as any click on any radio button (selected or not selected) would give the same result. I want to know how to know if the rating for that particular criteria had an input that was already checked and if so, uncheck it. I am completely lost at this point.</p>
  <p>I have the code provided below.</p>
  <p>HTML</p>
  <p>Please add this to the head section if you are testing on localhost for the 'star' icon to appear in the code</p>
  <pre><code>&lt;link href=&quot;https://fonts.googleapis.com/icon?family=Material+Icons&quot;
        rel=&quot;stylesheet&quot;&gt;
  
  </code></pre>
  <p>Here's the HTML</p>
  <pre><code>    &lt;div class=&quot;rating&quot;&gt;
        &lt;input id=&quot;rating1&quot; type=&quot;radio&quot; name=&quot;rating&quot; value=&quot;1&quot; /&gt;
        &lt;label for=&quot;rating1&quot; class=&quot;material-icons&quot;&gt;grade&lt;/label&gt;
        &lt;input id=&quot;rating2&quot; type=&quot;radio&quot; name=&quot;rating&quot; value=&quot;2&quot; /&gt;
        &lt;label for=&quot;rating2&quot; class=&quot;material-icons&quot;&gt;grade&lt;/label&gt;
        &lt;input id=&quot;rating3&quot; type=&quot;radio&quot; name=&quot;rating&quot; value=&quot;3&quot; /&gt;
        &lt;label for=&quot;rating3&quot; class=&quot;material-icons&quot;&gt;grade&lt;/label&gt;
        &lt;input id=&quot;rating4&quot; type=&quot;radio&quot; name=&quot;rating&quot; value=&quot;4&quot; /&gt;
        &lt;label for=&quot;rating4&quot; class=&quot;material-icons&quot;&gt;grade&lt;/label&gt;
        &lt;input id=&quot;rating5&quot; type=&quot;radio&quot; name=&quot;rating&quot; value=&quot;5&quot; /&gt;
        &lt;label for=&quot;rating5&quot; class=&quot;material-icons&quot;&gt;grade&lt;/label&gt;
      &lt;/div&gt;
  
  
  
  </code></pre>
  <p>Here's the CSS</p>
  <pre><code>input{
  /*   display:none; */
  }
  
  label{
    color:orange;
    font-size:3rem;
  }
  
  
  .rating:not(:hover) input:indeterminate + label{
    color:grey
  }
  
  .rating:not(:hover) input:checked ~ input + label{
    color:grey;
  }
  
  .rating input:hover ~ input +label{
    color:grey;
  }  
  
  
  
  /* .rating:not(:hover) input:focus-visible + label{
    color:orange
  } */
  
  /* Reference : https://iamkate.com/code/star-rating-widget/ */
  
  </code></pre>
  <p>And here's JS</p>
  <pre><code>const rating = document.querySelector('.rating');
  
  rating.addEventListener('change',function(){
    console.log('I have no idea what to do here!')
  })
  
  </code></pre>