How do you change the selection of an HTML radio object from Javascript?

I need to select an HTML converter (deselecting a previously selected radio) in my form from Javascript.

How is this achieved?

+4
source share
1 answer

If you set the "checked" property to true on one switch, another button with the same name will be automatically unchecked.

Thus,

 document.getElementById('buttonX').checked = true; 

will disable "buttonY" if the HTML looks like:

 <input type='radio' id='buttonX' name='fred' value='X'> <input type='radio' id='buttonY' name='fred' value='Y' checked> 

to change . Remember that the “radio button” has this name, because on the old radio stations (not necessarily older than me), the station preset buttons were mechanically interconnected, so exactly one button was pressed. To play with the buttons to get them all pressed was fun, but a risky pastime, since most adults did not appreciate the aesthetic appeal of a number of unmanageable radio books that were neatly aligned.

+8
source

Source: https://habr.com/ru/post/1308702/


All Articles