what I am trying to do is to write a function that selects a radio button, here is a codepen:
function select(valueOfRadio) {
$('#s1').removeAttr('checked')
$('#s2').removeAttr('checked')
$('#s3').removeAttr('checked')
switch (valueOfRadio) {// if it equals
case 2:
document.getElementById('s2').setAttribute('checked', true)
break;
case 3:
document.getElementById('s3').setAttribute('checked', true)
break;
default:
document.getElementById('s1').setAttribute('checked', true)
}
}
- the problem is that the `attr()` function doesn't work after pressing the radio button manually.
- after searching online I found that the attr() function works only to set initial values and that it can't be used after the user interacts with the element. so I tried the native `setAttribute()` but it didn't work either. then I found that I could use the jquery `val()` function, but I don't want to change the value of the radio button input, I want to change the `checked` attribute.
**my question is:**
how to change the checked attribute of radio button input without `attr()` function?