Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do select a radio button with JavaScript?

Tags:

javascript

I've tried select(); check(); and even .selected = true; but I still haven't gotten around to selecting that radio button. Is there any way I can accomplish this?

like image 839
David G Avatar asked May 26 '26 04:05

David G


2 Answers

You have to use the checked property:

element.checked = true;

Reference: HTMLInputElement

like image 171
Felix Kling Avatar answered May 27 '26 17:05

Felix Kling


<input type=radio id='r'>
<script>
document.getElementById('r').checked=true;
</script>

Use .checked

like image 28
Some Guy Avatar answered May 27 '26 17:05

Some Guy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!