Tuesday, July 23, 2019

[JavaScript][Resolved] Javascript click radiobox by value

<h1>st=0 </h1>
<input type="radio" name="st" value="0" /> 0 <br />
<input type="radio" name="st" value="1" /> 1 <br />
<input type="radio" name="st" value="3" /> 3 <br />
<input type="radio" name="st" value="4" /> 4 <br />
<input type="radio" name="st" value="5" /> 5 <br />

<script>
function clickRadioByValue(radioName,value){
    var nodes = document.querySelectorAll('input[name="'+radioName+'"]');
    for(var k in nodes){
 if(nodes[k].value == value){
            nodes[k].setAttribute("checked","checked");
        }
    }
}
clickRadioByValue("st",4);
</script>

Result :

you can try change the value 4 to 0,1,3 or 5 in clickRadioByValue("st",4); to test

2 comments :