How to get the selected radio button’s value with JavaScript?

Spread the love

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

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

How to get the selected radio button’s value with JavaScript?

To get the selected radio button’s value with JavaScript, we use querySelector.

For instance, we write

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

to call querySelector to select the radio button with name attribute set to genderS.

Then we get the checked radio button from the group with :checked.

And we get the value of its value attribute with value.

Conclusion

To get the selected radio button’s value with JavaScript, we use querySelector.

Leave a Reply

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