How to uncheck radio button javascript
Asked Answered
L

5

10

I have the below radio button. When it is clicked it is not possible to clear the radio button.

the code is

<td class="Label">
<input class="Label" type="radio" name="reviewoptions_{@id}" value="other" onClick="reAssign({count(../bankguaranteedata)},document.lending.guaranteereviewoptions,this,{@id})">
<xsl:if test="guaranteereviewoptions/reviewoptions[@id='other']='checked'">
<xsl:attribute name="checked"/>
</xsl:if>
</input>Other</td>

is it possible that when its clicked again it will clear the radio button?

Languet answered 3/4, 2013 at 10:12 Comment(3)
I suggest that you create a working example at jsbin.com. Thats going to make it much easier to help.Deidradeidre
created jsbin.com/oputew/1Languet
I think I'm just going to change it to a checkbox. makes more senseLanguet
T
4

It is not possible to de-select a radio button option in HTML, for some reasons why you might like to read this: https://ux.stackexchange.com/questions/13511/why-is-it-impossible-to-deselect-html-radio-inputs

Toccaratoccata answered 3/4, 2013 at 10:21 Comment(3)
I think I'm just going to change it to a checkbox. makes more senseLanguet
I agree. Never the less, @muneebShabbirs example should work.Deidradeidre
The question is "in JavaScript", not "in HTML".Offensive
T
15

i actually use this:

var elements = document.getElementsByTagName("input");

for (var i = 0; i < elements.length; i++) {
        if (elements[i].type == "radio") {
            elements[i].checked = false;
        }
    }
Toogood answered 3/3, 2020 at 15:16 Comment(1)
This is the simplest solution I've seen and it works perfectly. It is also easy to modify if you need to do that.Parted
T
4

It is not possible to de-select a radio button option in HTML, for some reasons why you might like to read this: https://ux.stackexchange.com/questions/13511/why-is-it-impossible-to-deselect-html-radio-inputs

Toccaratoccata answered 3/4, 2013 at 10:21 Comment(3)
I think I'm just going to change it to a checkbox. makes more senseLanguet
I agree. Never the less, @muneebShabbirs example should work.Deidradeidre
The question is "in JavaScript", not "in HTML".Offensive
I
2

try this

 if($('#checkboxID').is(':checked')){
         $('#checkboxID').attr('checked', false);
    }else{
        $('#checkboxID').attr('checked', true);
}

this Code is using jquery

Intrust answered 3/4, 2013 at 10:19 Comment(1)
true and false represent check and uncheckedIntrust
P
2

you might try using a reset input.

<input type='reset' />

call reset for the form radio buttons are in.

document.getElementById("radiobuttonsform").reset(); 
Phaih answered 3/4, 2013 at 10:20 Comment(0)
P
0

If you only require one radio option, then a checkbox would be the way to go. However, It is totally possible to deselect a radio option by virtue of its design. All you need is another radio option. You could use a silent radio option. It's only function in the group is to deselect the other radio options. You can change the opacity and hide it, and activate it with a button, or by some other means. This also leaves the ability to reselect one of the open options. Since options are typically grouped, other form elements will not be affected.

Putout answered 21/2 at 2:25 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Thirddegree

© 2022 - 2024 — McMap. All rights reserved.