Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to uncheck a radio button?

I have group of radio buttons that I want to uncheck after an AJAX form is submitted using jQuery. I have the following function:

function clearForm(){   $('#frm input[type="text"]').each(function(){       $(this).val("");     });   $('#frm input[type="radio":checked]').each(function(){       $(this).checked = false;     });  } 

With the help of this function, I can clear the values at the text boxes, but I can't clear the values of the radio buttons.

By the way, I also tried $(this).val(""); but that didn't work.

like image 265
systemsfault Avatar asked Jan 22 '10 13:01

systemsfault


People also ask

Can we uncheck radio button?

It is not a mandatory field. Radio button helps in ensuring only one option is selected. However, it doesn't allow user to deselect the option.

How do you deselect a radio button React?

To uncheck radio buttons in React, we can set the checked prop of the radio button to false . We have the checked state that we use to set the checked prop of each radio button. Then we set onChange to the changeRadio function.

How do I uncheck a radio button on my keyboard?

If you're on a radio or checkbox choice, just hit spacebar to select or unselect that active option.


2 Answers

either (plain js)

this.checked = false; 

or (jQuery)

$(this).prop('checked', false); // Note that the pre-jQuery 1.6 idiom was // $(this).attr('checked', false); 

See jQuery prop() help page for an explanation on the difference between attr() and prop() and why prop() is now preferable.
prop() was introduced with jQuery 1.6 in May 2011.

like image 155
David Hedlund Avatar answered Nov 06 '22 23:11

David Hedlund


You wouldn't need the each function

$("input:radio").attr("checked", false); 

Or

$("input:radio").removeAttr("checked"); 

The same should also apply to your textbox:

$('#frm input[type="text"]').val(""); 

But you could improve this

$('#frm input:text').val(""); 
like image 31
James Wiseman Avatar answered Nov 07 '22 01:11

James Wiseman