Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can you cancel the 'Checked' HTML property for a radio button using JavaScript?

Is it possible to cancel the checked HTML property for a radio button using JavaScript?

I have this layout:

<div class="wss_final_delivery">
  <div class="wss_final_delivery_wrap">Would you like to accept final delivery
    <label>
      <input type="radio" name="wss_accept_delivery" value="yes">
        Yes
    </label>
    <label>
      <input type="radio" name="wss_accept_delivery" value="no" checked="">
        No
    </label>
  </div>
</div>

This results in the "No" option to be checked by default. I would like none of the options to be checked by default and let the user choose his option. However, I can not change the HTML code. Is it possible to achieve using JavaSricpt?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can do it using javascript, like this:

 document.querySelectorAll("[name='wss_accept_delivery']").forEach(radio=> {
  radio.checked = false;
  radio.removeAttribute('checked')
 });
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading