1

Please advise, how to uncheck the radio button using javascript? I have checked similar questions but could not figure it out.

function uncheck1(){
    let chkstatus = document.getElementById("radiobtn1").checked;
    if (chkstatus==true) {
        document.getElementById("radiobtn1").checked = false;
    } else if (chkstatus==false){
        document.getElementById("radiobtn1").checked = true;
    } 
}
<input onclick="uncheck1()" id="radiobtn1" name="grp1" type="radio" value="prashant">prashant</input>

After click the condition is true and script is setting value to false so value is not getting selected at all. Please advise where exactly modification required?

1
  • 1
    If i understand your requirement , you will need a checkbox instead of radio butto Commented Sep 15, 2020 at 14:37

4 Answers 4

1

When you click a radio button, the onClick event will always think that the radio is checked, because that is how a radio works. Instead, use a second var to keep track of open status, and then set status accordingly....

var checked = false;
function uncheck1(){
    if(checked) {
         document.getElementById("radiobtn1").checked = false;
         checked = false;
         return;
    }
    checked = true;
}
<input onclick="uncheck1()" id="radiobtn1" name="grp1" type="radio" value="prashant">prashant</input>

I am now able to click the radio, see it get checked, click it again, and see it get unchecked.

Sign up to request clarification or add additional context in comments.

Comments

1

Add a button to toggle the radio button. You will see that it does work.

  <input type="button" onclick="uncheck1()" />

This is because you are toggling the radio onclick and doing the same action again with the function. So in fact you are toggling the checked status twice.

final code :

<input id="radiobtn1" name="grp1" type="radio" value="prashant" /> <label for="radiobtn1">prashant</label>

<input type="button" onclick="uncheck1()" />

Removed the onclick from the radio and added a proper label to make the radio work as expected.

2 Comments

Used a type="checkbox" insted of radio and it resolved my problem. Thanks Peter!
Thanks peter this is also working with adding extra button.
0

When you click a radio button, the onClick event will always think that the radio is checked, because that is how a radio works. Instead, use a second var to keep track of open status, and then set status accordingly....

var checked = false;
var checked2 = "";
function uncheck1(name){
    if(checked && checked2 == name) {
         document.getElementById(name).checked = false;
         checked = false;
         return;
    }
    checked = true;
    checked2 = name
}
<input type="radio" id="test1" name="test"onclick="uncheck1('test1')" value="1" />
<input type="radio" id="test2" name="test"onclick="uncheck1('test2')" value="2" />
<input type="radio" id="test3" name="test"onclick="uncheck1('test3')" value="3" />

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
0

Here's a different implementation which respects radio button groups (by name) and doesn't require any change to the literal <input type="radio"...> tag:

document.querySelectorAll("input[type=\"radio\"]").forEach((radio) => {
    radio.addEventListener("click", (event) => {
        if(event.target.dataset.checked == 'true') {
            event.target.checked = false;
            delete event.target.dataset.checked;
            event.target.dispatchEvent(new Event('change'));
        } else {
            for(const el of document.querySelectorAll(`input[name="${event.target.name}"]`))
                delete el.dataset.checked;
            event.target.dataset.checked = 'true';
        }
    });
});

1 Comment

Curious for feedback from downvoter

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.