Jquery, How to unselect all radio button in radio group

JqueryHtmlJquery MobileJquery Selectors

Jquery Problem Overview


Anyone know how to unselect all radio buttons in a radio group ?

HTML:

<div id="emptimfields">
	          <label id="lbl_emptim">How regulary do you employ people to help cultivate your land? </label><br/><br/>
	          
	         <fieldset data-role="controlgroup" data-type="vertical" id="emptim">
	          
	          <input name="emptim" id="radio1" value="fromtimetotime" type="radio" openmrs-valuecoded="" />
	          <label for="radio1"> From time to time </label>
	          
	          <input name="emptim" id="radio2" value="allthetime" type="radio" openmrs-valuecoded="" />
	          <label for="radio2">All the time</label>
	          
	          <input name="emptim" id="radio3" value="dontknow" type="radio" openmrs-valuecoded="" />
	          <label for="radio3"> Don't know </label> 
	        </fieldset>
        </div>

JQuery side:

$('input:radio[name=emptim]:checked').prop('checked', false); // doesn't work 

I'm certainly missing something basic, but I can't figure out what is the problem.

First, I check Yes and then check a value of the second radio group :

enter image description here

Then, I check No to hide the second radio group:

enter image description here

Then, If I click on next, I get the value of what I've checked (but I checked no previously, so here I don't want an alert, I want the radioButton "From time to time" to be unchecked) :

enter image description here

Finally, if I come back, nothing happend :

enter image description here

Jquery Solutions


Solution 1 - Jquery

You can give them a classname and try:

$('.classname').prop('checked', false);

When you use an older version of jQuery than 1.6 it has to be:

 $('<selector>').attr('checked', false);

EDIT :

Call the method .checkboxradio("refresh"); has also worked for me.

Solution 2 - Jquery

$('input:radio[name=indicatorType]').each(function () { $(this).prop('checked', false); });

Alternative solution for making 'unchecked' whole radio buttons of a radio group('indicatorType' for above code sample).

Solution 3 - Jquery

This should do it. Notice the refresh option at the end to force the controlgroup to be updated.

An additional benefit of this method is that it targets a specific radio group by name, rather than all radio controls on a page.

$('input:radio[name=emptim]:checked').prop('checked', false).checkboxradio("refresh");

Solution 4 - Jquery

I made a non-display radio button and called that via my PHP. You could use jquery to select the hidden button. I couldn't get the solutions here working, so I used some PHP, as I had to call the value from the DB. To help anyone, the solution I used is:

<input type="radio" id="test" value="" class = "timescale" name="timescale_radio_buttons" style="display: none" <?php $x = ((empty($gigObject->getTimescale())) || ($gigObject->getTimescale() == 'Blank')) ? 'checked="checked"' : ''; echo $x; ?>>

Solution 5 - Jquery

The prop doesnt works for me, bu this works perfectly :

('input:radio[name="RadioName"]').each(function () { $(this).attr('checked', false); });

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexis C.View Question on Stackoverflow
Solution 1 - JqueryRoelView Answer on Stackoverflow
Solution 2 - Jqueryuser1841830View Answer on Stackoverflow
Solution 3 - JqueryPhill HealeyView Answer on Stackoverflow
Solution 4 - JquerySpinstazView Answer on Stackoverflow
Solution 5 - JqueryJagganaView Answer on Stackoverflow