1

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?

JOKKER
  • 502
  • 6
  • 21
  • `document.querySelectorAll("[name='wss_accept_delivery']").forEach(function(item){item.checked = false;});` – Louys Patrice Bessette Dec 05 '21 at 16:14
  • 2
    [This](https://stackoverflow.com/questions/2554116/how-to-clear-radio-button-in-javascript) should provide additional information. – Kris West Dec 05 '21 at 16:16
  • 1
    Does this answer your question? [How to clear radio button in Javascript?](https://stackoverflow.com/questions/2554116/how-to-clear-radio-button-in-javascript) – Ivar Dec 05 '21 at 16:17

1 Answers1

1

You can do it using javascript, like this:

 document.querySelectorAll("[name='wss_accept_delivery']").forEach(radio=> {
  radio.checked = false;
  radio.removeAttribute('checked')
 });
Saeed Shamloo
  • 6,199
  • 1
  • 7
  • 18