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?

1

1 Answer 1

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) }); } 
Sign up to request clarification or add additional context in comments.

2 Comments

thanks Martin Sir for the answer with good explanation,it solved my issue ...thanks
if you want to do in one liner then $('input[name="choices"]:checked').prop("checked", false);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.