How to get the value of the selected radio button with JavaScript?

Spread the love

Sometimes, we want to get the value of the selected radio button with JavaScript.

In this article, we’ll look at how to get the value of the selected radio button with JavaScript.

How to get the value of the selected radio button with JavaScript?

To get the value of the selected radio button with JavaScript, we select checked radio button.

For instance, we write

const value = document.querySelector('input[name="rate"]:checked').value;

to select the checked radio button with the name attribute rate by calling querySelector with 'input[name="rate"]:checked'.

Then we get its value property to get the checked radio button’s value.

Conclusion

To get the value of the selected radio button with JavaScript, we select checked radio button.

Leave a Reply

Your email address will not be published. Required fields are marked *