jQuery has :checked
selector which works for checkboxes, radio buttons, and options of select elements. It returns elements that havechecked
attribute as true.
In combination with val() method we can retrieve value of selected radio button.
<h3>Your favorite aquatic sports:</h3> <label><input type="radio" name="sports" value="bodyboarding">bodyboarding</label><br/> <label><input type="radio" name="sports" value="diving">diving</label><br/> <label><input type="radio" name="sports" value="rowing">rowing</label><br/> <label><input type="radio" name="sports" value="synchronized_swimming">synchronized swimming</label> <p><input type="button" id="submit" value="Submit"></p>
$(document).ready(function(){ $("#submit").on('click', function(){ var selectedSports = $("input[name='sports']:checked").val(); if(selectedSports){ console.log("Your choice is " + selectedSports); } }); });