Get Checked Radio Button Value By Name jQuery

In this tutorial we will learn how to Get Checked Radio Button Value By Name in jQuery. jQuery val() method, Attribute Equals Selector and :checked Selector are used to get the value of checked radio button by it's name.

jQuery val() Method

jQuery val() Method sets or returns the value of input HTML elements.

In this example val() method is used to get the value of checked radio button using it's name.

Attribute Equals Selector

Attribute Equals Selector is a selector used to select HTML elements whose defined attributes have exact defined values.

In this example Attribute Equals Selector is [name=color].

:checked Selector

:checked Selector is used to target or select the checked radio buttons and checkboxes.

In this example :checked Selector is used to select the checked radio button to get it's value.

HTML Code

HTML Code is given below, in this code we have two radio buttons with two labels and a button tag.

<!DOCTYPE html>
<html>
<head>
<title>Get Checked Radio Button Value By Name jQuery</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>    
</head>
<body> 
    
<label for="r1">Red</label>
<input id="r1" type="radio" value='red' name="color"> 
<label for="b1">Blue</label>
<input id="b1" type="radio" value='blue' name="color">
<button id='btn'>Check Radio Value</button>
    
<script src="script.js"></script>    
</body>
</html>

JQuery Code

JQuery Code is given below, the code is executed whenever button is clicked. This is done using jQuery click() method.

$('input[name=color]:checked') is used to select the checked radio button with specific name. While val() method is used to get the value of that checked radio button.

<script>
$(document).ready(function(){
    $('#btn').click(function(){
      var value = $('input[name=color]:checked').val();  
      alert(value);    
    });
});
</script>

Demo

Video Tutorial

Watch video tutorial on How To Get Checked Radio Button Value By Name in jQuery.

How To Fade in Image with jQuery How To Change image src with jQuery How To Display Message on Mouse Enter and Leave Events How To Scroll Page to the Bottom using jQuery How To Scroll Page to the Top using jQuery Check radio button Dynamically with jQuery Change Background Color on hover with jQuery Hide and Show div using jQuery Get value of input element with jQuery Remove last child of parent element with jQuery Add li tag to the end of list jQuery Delete first row of Table with jQuery Delete last row of Table with jQuery Add first list item using jQuery jQuery Get id of Button jQuery toggle show hide on click Disable Button after Click with jQuery Get Button Text with jQuery jQuery Dynamic li Click Event How To Remove all Spaces in String with jQuery Get Selected file name in jQuery Remove Last Option of Select tag with jQuery Remove First Option from Select tag in jQuery jQuery Capitalize First Letter of String jQuery Check input value length