HTML INPUT RADIO empty the value of a radio button with JavaScript

-2

Good day I'm working on a SELECT input which, according to the element, displays a different Radio Button group but I need the radiobutton not to be selected when it shows a new group, all radios are deselected and I want to do it with JavaScript.

    
asked by jose marquez 13.03.2018 в 00:12
source

1 answer

1

Although when you start the page there is no one selected, once you have selected one you have to deselect them manually. Here is an example:

HTML

<input id="rad1" type="radio" name="grpRadio">
<input id="rad2" type="radio" name="grpRadio">
<input id="rad3" type="radio" name="grpRadio">
<input id="btnReset" type="button" value="Reset">

JS

var btnReset = document.getElementById("btnReset");
if (btnReset) {
    btnReset.onclick = function () {
        resetRadioButtons("grpRadio");
    }
}
else {
    alert("btnReset not found");
}

function resetRadioButtons(groupName) {
    var arRadioBtn = document.getElementsByName(groupName);

    for (var ii = 0; ii < arRadioBtn.length; ii++) {
        var radButton = arRadioBtn[ii];
        radButton.checked = false;
    }
}

I hope it serves you.

    
answered by 14.03.2018 / 12:54
source