This post will discuss how to get the value of the selected radio button in JavaScript and jQuery.

To get the selected radio button, you can use the :checked CSS pseudo-class selector, representing any radio (<input type="radio">) that is checked. We can use this in many ways with jQuery and plain JavaScript.

1. Using jQuery

1. The .val() method can be used to get the value of matched radio elements. When no options are selected, it returns an undefined value.

The following code uses .is() inside an event handler and returns true if any radio button is selected.

JS


HTML



Edit in JSFiddle

 
2. jQuery’s .filter() method takes a selector expression and matches it against the current set of elements.

JS


HTML



Edit in JSFiddle

2. Using JavaScript

With JavaScript, you can loop through each <radio> button using for…of loop:

JS


HTML



Edit in JSFiddle

 
This can be shortened to the following code:

JS


HTML



Edit in JSFiddle

 
Alternatively, you can use the querySelector() method to access the matched element.

JS


HTML



Edit in JSFiddle

That’s all about getting the value of the selected radio button in JavaScript and jQuery.