In jQuery, to know which radio button is selected, we first need to find the desired input group. We can achieve this by using the name
attribute of the input
element as below.
$('input[name=gender]:checked', formId)
The above will select all the input groups that have the name
attribute as gender
in the specified form. We can then use the val()
method to get the value of the selected radio button as below.
$('input[name=gender]:checked', formId).val()
In the HTML tab,
jQuery
package.id='example'
.input
elements of type radio
with the name
attribute as gender
.id='btn'
.In the JavaScript tab,
click
event on the button with id='btn'
.value
.value
on the console.When the button is clicked, the value
of the selected radio button will be printed on the console.