In this post, we will explore different ways of determining whether a radio button is selected or not with JavaScript and jQuery.

1. Using checked property

One of the simplest and most intuitive ways of determining whether a radio button is selected or not with JavaScript is by using the checked property of the radio button element. The checked property returns a boolean value that indicates whether the radio button is selected (true) or not (false). For example, suppose we have the following HTML code:

 
We can use the following JavaScript code to check whether the radio button with id "email" is selected or not:

 
We can also use the same approach with jQuery by using the prop() function. This function can either get or set the value of a property for the matched elements. We can get the radio button element by its id using the jQuery selector, and then use the prop() function with the checked property, which returns the same value as the checked property of the element. For example, the following jQuery code checks whether the radio button with id "email" is selected or not:

2. Using :checked selector

Another way to check whether a radio button is selected or not is to use the :checked selector. The :checked selector matches all elements that are checked, such as checkboxes, radio buttons, and option elements. We can use the following JavaScript code to check which radio button with name "contact" is selected:

 
We can also use the same approach with jQuery by using the .is(selector) function. This function takes a selector, a filter function, or an element as a parameter, and returns true if the current jQuery object matches the parameter, and false otherwise. We can get the radio button element by its id using the jQuery selector, and then use the is() function with the :checked selector, as follows:

That’s all about determining whether a radio button is selected in JavaScript and jQuery.