1

I have this javascript code to uncheck the radio button which is working fine

function uncheckRadio(){
    const radioBtns = document.querySelectorAll('input[name="choices"]');
    for(const btn of radioBtns){
        if(btn.checked){
            btn.checked = false;
        }
    }
}

What would this function look like if rewritten into jQuery?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Deviprasad Sharma
  • 470
  • 1
  • 8
  • 19
  • 1
    Does this answer your question? [How to uncheck a radio button?](https://stackoverflow.com/questions/2117538/how-to-uncheck-a-radio-button) – djcaesar9114 Jun 28 '20 at 11:37

1 Answers1

1

First you need to think of the selector in jQuery. The selector in your case is the name.

Secondly, you need to think of a way to loop through your elements. This can be done by the .each() function in jQuery.

Use the .prop() function to uncheck the radio button by setting the property to false.

jQuery Example:

function uncheckRadio(){
    $('input[name^="choices"]').each(function() {
        $(this).prop('checked', false)
    });
}
Martin
  • 2,326
  • 1
  • 12
  • 22