Get the value of selected radio button in jQuery

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);
	    }
	});
});

Leave a Comment

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

We care about your privacy and data security. Google serves cookies to analyse traffic and show ads on this site. By using this site you agree to these cookies being set. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close