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

Time:12-06

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>
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

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?

CodePudding user response:

You can do it using javascript, like this:

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